Maximum area rectangle by picking four sides from array
Last Updated :
19 Sep, 2023
Given an array of n positive integers that represent lengths. Find out the maximum possible area whose four sides are picked from the given array. Note that a rectangle can only be formed if there are two pairs of equal values in the given array.
Examples:
Input : arr[] = {2, 1, 2, 5, 4, 4}
Output : 8
Explanation : Dimension will be 4 * 2
Input : arr[] = {2, 1, 3, 5, 4, 4}
Output : 0
Explanation : No rectangle possible
Method 1 (Sorting): The task basically reduces to finding two pairs of equal values in the array. If there are more than two pairs, then pick the two pairs with maximum values. A simple solution is to do the following.
- Sort the given array.
- Traverse array from largest to smallest value and return two pairs with maximum values.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findArea( int arr[], int n)
{
sort(arr, arr + n, greater< int >());
int dimension[2] = { 0, 0 };
for ( int i = 0, j = 0; i < n - 1 && j < 2; i++)
if (arr[i] == arr[i + 1])
dimension[j++] = arr[i++];
return (dimension[0] * dimension[1]);
}
int main()
{
int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findArea(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
public class GFG
{
static int findArea(Integer arr[], int n)
{
Arrays.sort(arr, Collections.reverseOrder());
int [] dimension = { 0 , 0 };
for ( int i = 0 , j = 0 ; i < n - 1 && j < 2 ;
i++)
if (arr[i] == arr[i + 1 ])
dimension[j++] = arr[i++];
return (dimension[ 0 ] * dimension[ 1 ]);
}
public static void main(String args[])
{
Integer arr[] = { 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 };
int n = arr.length;
System.out.println(findArea(arr, n));
}
}
|
Python3
def findArea(arr, n):
arr.sort(reverse = True )
dimension = [ 0 , 0 ]
i = 0
j = 0
while (i < n - 1 and j < 2 ):
if (arr[i] = = arr[i + 1 ]):
dimension[j] = arr[i]
j + = 1
i + = 1
i + = 1
return (dimension[ 0 ] *
dimension[ 1 ])
arr = [ 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 ]
n = len (arr)
print (findArea(arr, n))
|
C#
using System;
using System.Collections;
class GFG
{
static int findArea( int []arr, int n)
{
Array.Sort(arr);
Array.Reverse(arr);
int [] dimension = { 0, 0 };
for ( int i = 0, j = 0;
i < n - 1 && j < 2; i++)
if (arr[i] == arr[i + 1])
dimension[j++] = arr[i++];
return (dimension[0] * dimension[1]);
}
public static void Main(String []args)
{
int []arr = { 4, 2, 1, 4, 6, 6, 2, 5 };
int n = arr.Length;
Console.Write(findArea(arr, n));
}
}
|
PHP
<?php
function findArea( $arr , $n )
{
rsort( $arr );
$dimension = array ( 0, 0 );
for ( $i = 0, $j = 0; $i < $n - 1 &&
$j < 2; $i ++)
if ( $arr [ $i ] == $arr [ $i + 1])
$dimension [ $j ++] = $arr [ $i ++];
return ( $dimension [0] *
$dimension [1]);
}
$arr = array (4, 2, 1, 4, 6, 6, 2, 5);
$n = count ( $arr );
echo findArea( $arr , $n );
?>
|
Javascript
<script>
function findArea(arr, n)
{
arr.sort((a,b)=>{ return b-a;})
var dimension = [0,0];
for ( var i = 0, j = 0; i < n - 1 && j < 2; i++)
if (arr[i] == arr[i + 1])
dimension[j++] = arr[i++];
return (dimension[0] * dimension[1]);
}
var arr = [ 4, 2, 1, 4, 6, 6, 2, 5 ];
var n = arr.length;
document.write( findArea(arr, n));
</script>
|
Time Complexity: O(n Log n) as one traversal of array takes O(n) time and sorting the array takes n logn extra time , hence overall time taken by the algorithm is O(n Log n)
Auxiliary Space: O(1) since no extra array is used so it takes constant space
Method 2 (Hashing): The idea is to insert all first occurrences of elements in a hash set. For second occurrences, keep track of the maximum of two values.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findArea( int arr[], int n)
{
unordered_set< int > s;
int first = 0, second = 0;
for ( int i = 0; i < n; i++) {
if (s.find(arr[i]) == s.end()) {
s.insert(arr[i]);
continue ;
}
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second)
second = arr[i];
}
return (first * second);
}
int main()
{
int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findArea(arr, n);
return 0;
}
|
Java
import java.util.HashSet;
import java.util.Set;
public class GFG
{
static int findArea( int arr[], int n)
{
Set<Integer> s = new HashSet<>();
int first = 0 , second = 0 ;
for ( int i = 0 ; i < n; i++) {
if (!s.contains(arr[i])) {
s.add(arr[i]);
continue ;
}
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second)
second = arr[i];
}
return (first * second);
}
public static void main(String args[])
{
int arr[] = { 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 };
int n = arr.length;
System.out.println(findArea(arr, n));
}
}
|
Python3
C#
Javascript
Time Complexity: O(n) as only one traversal of array is required to complete all operations hence overall complexity turns out be O(n)
Auxiliary Space: O(n) since an unordered set is used, in worst case all elements will be inserted into the set thus occupying O(n) space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...