- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The area of the rectangle is calculated as a product of its sides. All rectangles have four sides such that opposite sides are equal. For calculating the area we require length and breadth as two sides. So that we can get desired result −

Area rectangle = length X breadth

We are given an array such that it consists of sides of a rectangle. The array contains values for all four sides in random order. The task here is to find two highest pairs of sides from the array in order to get the maximum area possible for the rectangle.

**Input**

Arr[] = { 1,2,1,3,4,4,2,3,5,7 }.

**Output** − Maximum area rectangle by picking four sides from array − 12

**Explanation** − If we sort the given array in descending order we get,

Arr[]= { 7,5,4,4,3,3,2,1,1 }

Two pairs of sides ( total four sides ) that are maximum here is { (4,4),(3,3) }. Therefore the desired maximum area possible is 12 sq units.

**Input**

Arr[] = { 8,2,5,3,4,9,8,3,5,7 }.

**Output** − Maximum area rectangle by picking four sides from array − 40

**Explanation** − If we sort the given array in descending order we get,

Arr[]= { 9,8,8,7,5,5,4,3,3,2 }

Two pairs of sides ( total four sides ) that are maximum here is { (8,8),(5,5) }. Therefore the desired maximum area possible is 40 sq units.

Declare an array of integers which contains pairs of sides of rectangle.( Arr[] )

Create a variable to store the size of the array. (n)

The function maxArea(int arr[],int n) is used to calculate the maximum area for the rectangle. It takes an input array and its size as arguments.

Inside maxArea() we declared an array Dim[2] two store highest two sides found after traversing the sorted array (in descending order) arr[].

As arr[] is sorted in descending order the highest 4 sides must be in the beginning. We iterate arr[] such that a pair of sides is found.

Initialize Dim[] with 0 at first.

Inside the while loop we put the condition that it continues till j<2 that there are no values found for dim[0] and dim[1] or the end of arr[] is reached. (i<n).

If a pair of such sides is found, ( if(arr[i]==arr[i+1]) ), then store it in dim[j] and increment j for next side.

Return the result as a product of dim[0] and dim[1].

Note − sort(arr,n) is supposed to sort arr in descending order.

#include <iostream> using namespace std; // function for finding max area for rectangle int maxArea(int arr[], int n){ int dim[2]={0}; int i=0,j=0; while( j<2 && i<n){ if(arr[i]==arr[i+1]){ dim[j++]=arr[i]; } ++i; } return dim[0]*dim[1]; } // driver function int main(){ int arr[] = { 1,8,5,1,8,2,5,3 }; int n = 8; sort(arr,n); // supposed to sort array in descending order cout <<”Maximum area of rectangle by picking four sides from array:”<< maxArea(arr, n); return 0; }

If we run the above code it will generate the following output −

Maximum area of rectangle by picking four sides from array: 40

- Related Questions & Answers
- Check if given four integers (or sides) make rectangle in Python
- Rectangle Area in C++
- Maximum sum by picking elements from two arrays in order in C++ Program
- Maximum area of rectangle possible with given perimeter in C++
- Rectangle Area II in C++
- Picking index randomly from array in JavaScript
- Picking out uniques from an array in JavaScript
- C program to find maximum of four integers by defining function
- Picking the largest elements from multidimensional array in JavaScript
- C Program for Area And Perimeter Of Rectangle
- Maximum possible time that can be formed from four digits in C++
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed in C++
- Maximum area of quadrilateral in C++
- Area of the circumcircle of any triangles with sides given in C++
- Maximum Perimeter Triangle from array in C++

Advertisements