Sum of Areas of Rectangles possible for an array
Last Updated :
23 Feb, 2022
Given an array, the task is to compute the sum of all possible maximum area rectangles which can be formed from the array elements. Also, you can reduce the elements of the array by at most 1.
Examples:
Input: a = {10, 10, 10, 10, 11,
10, 11, 10}
Output: 210
Explanation:
We can form two rectangles one square (10 * 10)
and one (11 * 10). Hence, total area = 100 + 110 = 210.
Input: a = { 3, 4, 5, 6 }
Output: 15
Explanation:
We can reduce 4 to 3 and 6 to 5 so that we got
rectangle of (3 * 5). Hence area = 15.
Input: a = { 3, 2, 5, 2 }
Output: 0
Naive Approach: Check for all possible four elements of the array and then whichever can form a rectangle. In these rectangles, separate all those rectangles which are of the maximum area formed by these elements. After getting the rectangles and their areas, sum them all to get our desired solution.
Efficient Approach: To get the maximum area rectangle, first sort the elements of the array in the non-increasing array. After sorting, start the procedure to select the elements of the array. Here, selection of two elements of array (as length of rectangle) is possible if elements of array are equal (a[i] == a[i+1]) or if length of smaller element a[i+1] is one less than a[i] (in this case we have our length a[i+1] because a[i] is decreased by 1). One flag variable is maintained to check that whether we get length and breadth both. After getting the length, set the flag variable, now calculate the breadth in the same way as we have done for length, and sum the area of the rectangle. After getting length and breadth both, again set the flag variable false so that we will now search for a new rectangle. This process is repeated and lastly, the final sum of the area is returned.
C++
#include <bits/stdc++.h>
using namespace std;
int MaxTotalRectangleArea( int a[],
int n)
{
sort(a, a + n, greater< int >());
int sum = 0;
bool flag = false ;
int len;
for ( int i = 0; i < n; i++)
{
if ((a[i] == a[i + 1] || a[i] -
a[i + 1] == 1) && (!flag))
{
flag = true ;
len = a[i + 1];
i++;
}
else if ((a[i] == a[i + 1] ||
a[i] - a[i + 1] == 1) && (flag))
{
sum = sum + a[i + 1] * len;
flag = false ;
i++;
}
}
return sum;
}
int main()
{
int a[] = { 10, 10, 10, 10,
11, 10, 11, 10,
9, 9, 8, 8 };
int n = sizeof (a) / sizeof (a[0]);
cout << MaxTotalRectangleArea(a, n);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
import java.util.*;
class GFG
{
static int MaxTotalRectangleArea(Integer []a,
int n)
{
Arrays.sort(a, Collections.reverseOrder());
int sum = 0 ;
boolean flag = false ;
int len = 0 ;
for ( int i = 0 ; i < n; i++)
{
if ((a[i] == a[i + 1 ] ||
a[i] - a[i+ 1 ] == 1 ) &&
!flag)
{
flag = true ;
len = a[i+ 1 ];
i++;
}
else if ((a[i] == a[i + 1 ] ||
a[i] - a[i+ 1 ] == 1 ) &&
(flag))
{
sum = sum + a[i+ 1 ] * len;
flag = false ;
i++;
}
}
return sum;
}
public static void main (String args[])
{
Integer []a = { 10 , 10 , 10 , 10 ,
11 , 10 , 11 , 10 ,
9 , 9 , 8 , 8 };
int n = a.length;
System.out.print(MaxTotalRectangleArea(a, n));
}
}
|
Python3
def MaxTotalRectangleArea(a, n) :
a.sort(reverse = True )
sum = 0
flag = False
len = 0
i = 0
while (i < n - 1 ) :
if (i ! = 0 ) :
i = i + 1
if ((a[i] = = a[i + 1 ] or
a[i] - a[i + 1 ] = = 1 )
and flag = = False ) :
flag = True
len = a[i + 1 ]
i = i + 1
elif ((a[i] = = a[i + 1 ] or
a[i] - a[i + 1 ] = = 1 )
and flag = = True ) :
sum = sum + a[i + 1 ] * len
flag = False
i = i + 1
return sum
a = [ 10 , 10 , 10 , 10 , 11 , 10 ,
11 , 10 , 9 , 9 , 8 , 8 ]
n = len (a)
print (MaxTotalRectangleArea(a, n))
|
C#
using System;
class GFG {
static int MaxTotalRectangleArea( int []a,
int n)
{
Array.Sort(a);
Array.Reverse(a);
int sum = 0;
bool flag = false ;
int len =0;
for ( int i = 0; i < n; i++)
{
if ((a[i] == a[i + 1] || a[i] -
a[i + 1] == 1) && (!flag))
{
flag = true ;
len = a[i + 1];
i++;
}
else if ((a[i] == a[i + 1] ||
a[i] - a[i + 1] == 1) && (flag))
{
sum = sum + a[i + 1] * len;
flag = false ;
i++;
}
}
return sum;
}
static public void Main ()
{
int []a = { 10, 10, 10, 10,
11, 10, 11, 10,
9, 9, 8, 8 };
int n = a.Length;
Console.WriteLine(
MaxTotalRectangleArea(a, n));
}
}
|
PHP
<?php
function MaxTotalRectangleArea( $a , $n )
{
rsort( $a );
$sum = 0;
$flag = false;
$len ;
for ( $i = 0; $i < $n ; $i ++)
{
if (( $a [ $i ] == $a [ $i + 1] or $a [ $i ] -
$a [ $i + 1] == 1) and (! $flag ))
{
$flag = true;
$len = $a [ $i + 1];
$i ++;
}
else if (( $a [ $i ] == $a [ $i + 1] or
$a [ $i ] - $a [ $i + 1] == 1) and
( $flag ))
{
$sum = $sum + $a [ $i + 1] * $len ;
$flag = false;
$i ++;
}
}
return $sum ;
}
$a = array ( 10, 10, 10, 10,
11, 10, 11, 10,
9, 9, 8, 8 );
$n = count ( $a );
echo MaxTotalRectangleArea( $a , $n );
?>
|
Javascript
<script>
function MaxTotalRectangleArea( a, n)
{
a.sort();
a.reverse();
let sum = 0;
let flag = false ;
let len;
for (let i = 0; i < n; i++)
{
if ((a[i] == a[i + 1] || a[i] -
a[i + 1] == 1) && (!flag))
{
flag = true ;
len = a[i + 1];
i++;
}
else if ((a[i] == a[i + 1] ||
a[i] - a[i + 1] == 1) && (flag))
{
sum = sum + a[i + 1] * len;
flag = false ;
i++;
}
}
return sum;
}
let a = [ 10, 10, 10, 10,
11, 10, 11, 10,
9, 9, 8, 8 ];
let n = a.length;
document.write(MaxTotalRectangleArea(a, n));
</script>
|
Time Complexity: O(nlog(n))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...