给定一个整数N和一个大小为4 * N的数组arr [] ,任务是检查如果每个元素只能使用一次,则是否可以从该数组中形成N个相等面积的矩形。
例子:
Input: arr[] = {1, 8, 2, 1, 2, 4, 4, 8}, N = 2
Output: Yes
Two rectangles with sides (1, 8, 1, 8) and (2, 4, 2, 4) can be formed.
Both of these rectangles have the same area.
Input: arr[] = {1, 3, 3, 5, 5, 7, 1, 6}, N = 2
Output: No
方法:
- 需要四个边来形成一个矩形。
- 给定4 * N个整数,最多只能使用一次数字形成N个矩形。
- 任务是检查所有矩形的面积是否相同。为了检查这一点,首先对数组进行排序。
- 侧面被视为前两个元素和后两个元素。
- 计算并检查面积是否与初始计算的面积相同。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to check whether we can make n
// rectangles of equal area
bool checkRectangles(int* arr, int n)
{
bool ans = true;
// Sort the array
sort(arr, arr + 4 * n);
// Find the area of any one rectangle
int area = arr[0] * arr[4 * n - 1];
// Check whether we have two equal sides
// for each rectangle and that area of
// each rectangle formed is the same
for (int i = 0; i < 2 * n; i = i + 2) {
if (arr[i] != arr[i + 1]
|| arr[4 * n - i - 1] != arr[4 * n - i - 2]
|| arr[i] * arr[4 * n - i - 1] != area) {
// Update the answer to false
// if any condition fails
ans = false;
break;
}
}
// If possible
if (ans)
return true;
return false;
}
// Driver code
int main()
{
int arr[] = { 1, 8, 2, 1, 2, 4, 4, 8 };
int n = 2;
if (checkRectangles(arr, n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to check whether we can make n
// rectangles of equal area
static boolean checkRectangles(int[] arr, int n)
{
boolean ans = true;
// Sort the array
Arrays.sort(arr);
// Find the area of any one rectangle
int area = arr[0] * arr[4 * n - 1];
// Check whether we have two equal sides
// for each rectangle and that area of
// each rectangle formed is the same
for (int i = 0; i < 2 * n; i = i + 2)
{
if (arr[i] != arr[i + 1] ||
arr[4 * n - i - 1] != arr[4 * n - i - 2] ||
arr[i] * arr[4 * n - i - 1] != area)
{
// Update the answer to false
// if any condition fails
ans = false;
break;
}
}
// If possible
if (ans)
return true;
return false;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 8, 2, 1, 2, 4, 4, 8 };
int n = 2;
if (checkRectangles(arr, n))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python implementation of the approach
# Function to check whether we can make n
# rectangles of equal area
def checkRectangles(arr, n):
ans = True
# Sort the array
arr.sort()
# Find the area of any one rectangle
area = arr[0] * arr[4 * n - 1]
# Check whether we have two equal sides
# for each rectangle and that area of
# each rectangle formed is the same
for i in range(0, 2 * n, 2):
if (arr[i] != arr[i + 1]
or arr[4 * n - i - 1] != arr[4 * n - i - 2]
or arr[i] * arr[4 * n - i - 1] != area):
# Update the answer to false
# if any condition fails
ans = False
break
# If possible
if (ans):
return True
return False
# Driver code
arr = [ 1, 8, 2, 1, 2, 4, 4, 8 ]
n = 2
if (checkRectangles(arr, n)):
print("Yes")
else:
print("No")
# This code is contributed by Sanjit_Prasad
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to check whether we can make n
// rectangles of equal area
static bool checkRectangles(int[] arr, int n)
{
bool ans = true;
// Sort the array
Array.Sort(arr);
// Find the area of any one rectangle
int area = arr[0] * arr[4 * n - 1];
// Check whether we have two equal sides
// for each rectangle and that area of
// each rectangle formed is the same
for (int i = 0; i < 2 * n; i = i + 2)
{
if (arr[i] != arr[i + 1] ||
arr[4 * n - i - 1] != arr[4 * n - i - 2] ||
arr[i] * arr[4 * n - i - 1] != area)
{
// Update the answer to false
// if any condition fails
ans = false;
break;
}
}
// If possible
if (ans)
return true;
return false;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 8, 2, 1, 2, 4, 4, 8 };
int n = 2;
if (checkRectangles(arr, n))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
Yes