- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

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

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

- Related Articles
- Maximum sum by picking elements from two arrays in order in C++ Program
- Picking index randomly from array in JavaScript
- Check if given four integers (or sides) make rectangle in Python
- Picking out uniques from an array in JavaScript
- C++ Program to get maximum area of rectangle made from line segments
- Picking the largest elements from multidimensional array in JavaScript
- Maximum area of rectangle possible with given perimeter in C++
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed in C++
- Rectangle Area in C++
- Picking the triangle edges with maximum perimeter JavaScript
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array in C++
- Rectangle Area II in C++
- Maximum Perimeter Triangle from array in C++
- Maximum possible time that can be formed from four digits in C++
- Find maximum points which can be obtained by deleting elements from array in Python