# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// 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 * 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 `

## Python

 `# 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 * 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 `

Output:

```Yes
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.