Maximum area rectangle by picking four sides from array in C++

C++Server Side ProgrammingProgramming

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.

Approach used in the below program is as follows

  • 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.

Example

#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;
}

Output

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

Maximum area of rectangle by picking four sides from array: 40
raja
Published on 14-Aug-2020 11:20:14
Advertisements