Check if N rectangles of equal area can be formed from (4 * N) integers
Given an integer N and an array arr[] of size 4 * N, the task is to check whether N rectangles of equal area can be formed from this array if each element can be used only once.
Examples:
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
Approach:
- Four sides are needed to form a rectangle.
- Given 4 * N integers, utmost N rectangles can be formed using numbers only once.
- The task is to check if the areas of all the rectangles are same. To check this, the array is first sorted.
- The sides are considered as the first two elements and the last two elements.
- Area is calculated and checked if it has the same area as the initially calculated area.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkRectangles( int * arr, int n)
{
bool ans = true ;
sort(arr, arr + 4 * n);
int area = arr[0] * arr[4 * n - 1];
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) {
ans = false ;
break ;
}
}
if (ans)
return true ;
return false ;
}
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
import java.util.*;
class GFG
{
static boolean checkRectangles( int [] arr, int n)
{
boolean ans = true ;
Arrays.sort(arr);
int area = arr[ 0 ] * arr[ 4 * n - 1 ];
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)
{
ans = false ;
break ;
}
}
if (ans)
return true ;
return false ;
}
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" );
}
}
|
Python3
def checkRectangles(arr, n):
ans = True
arr.sort()
area = arr[ 0 ] * arr[ 4 * n - 1 ]
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):
ans = False
break
if (ans):
return True
return False
arr = [ 1 , 8 , 2 , 1 , 2 , 4 , 4 , 8 ]
n = 2
if (checkRectangles(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool checkRectangles( int [] arr, int n)
{
bool ans = true ;
Array.Sort(arr);
int area = arr[0] * arr[4 * n - 1];
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)
{
ans = false ;
break ;
}
}
if (ans)
return true ;
return false ;
}
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" );
}
}
|
Javascript
<script>
function checkRectangles(arr, n)
{
let ans = true ;
arr.sort();
var area = arr[0] * arr[4 * n - 1];
for (let 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)
{
ans = false ;
break ;
}
}
if (ans)
return true ;
return false ;
}
var arr = [ 1, 8, 2, 1, 2, 4, 4, 8 ];
var n = 2;
if (checkRectangles(arr, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n * log n) //the time complexity for using inbuilt sort function is O(N*logN).
Auxiliary Space: O(1), as constant space is used.
Last Updated :
30 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...