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:210Explanation: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:15Explanation: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 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, final sum of the area is returned.

## C++

`// CPP code to find sum of all` `// area rectangle possible` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find ` `// area of rectangles` `int` `MaxTotalRectangleArea(` `int` `a[], ` ` ` `int` `n)` `{` ` ` `// sorting the array in` ` ` `// descending order` ` ` `sort(a, a + n, greater<` `int` `>());` ` ` `// store the final sum of` ` ` `// all the rectangles area` ` ` `// possible` ` ` `int` `sum = 0;` ` ` `bool` `flag = ` `false` `;` ` ` `// temporary variable to store` ` ` `// the length of rectangle` ` ` `int` `len;` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{` ` ` ` ` `// Selecting the length of ` ` ` `// rectangle so that difference ` ` ` `// between any two number is 1 ` ` ` `// only. Here length is selected ` ` ` `// so flag is set` ` ` `if` `((a[i] == a[i + 1] || a[i] - ` ` ` `a[i + 1] == 1) && (!flag)) ` ` ` `{` ` ` `// flag is set means` ` ` `// we have got length of` ` ` `// rectangle` ` ` `flag = ` `true` `;` ` ` `// length is set to` ` ` `// a[i+1] so that if ` ` ` `// a[i] a[i+1] is less ` ` ` `// than by 1 then also` ` ` `// we have the correct ` ` ` `// choice for length` ` ` `len = a[i + 1];` ` ` `// incrementing the counter ` ` ` `// one time more as we have ` ` ` `// considered a[i+1] element ` ` ` `// also so.` ` ` `i++;` ` ` `}` ` ` `// Selecting the width of rectangle` ` ` `// so that difference between any` ` ` `// two number is 1 only. Here width` ` ` `// is selected so now flag is again ` ` ` `// unset for next rectangle` ` ` `else` `if` `((a[i] == a[i + 1] || ` ` ` `a[i] - a[i + 1] == 1) && (flag)) ` ` ` `{` ` ` `// area is calculated for` ` ` `// rectangle` ` ` `sum = sum + a[i + 1] * len;` ` ` `// flag is set false` ` ` `// for another rectangle` ` ` `// which we can get from` ` ` `// elements in array` ` ` `flag = ` `false` `;` ` ` `// incrementing the counter ` ` ` `// one time more as we have ` ` ` `// considered a[i+1] element ` ` ` `// also so.` ` ` `i++;` ` ` `}` ` ` `}` ` ` `return` `sum;` `}` `// Driver code` `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;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java code to find sum of ` `// all area rectangle possible` `import` `java.io.*;` `import` `java.util.Arrays;` `import` `java.util.*;` `class` `GFG ` `{` ` ` `// Function to find ` ` ` `// area of rectangles` ` ` `static` `int` `MaxTotalRectangleArea(Integer []a, ` ` ` `int` `n)` ` ` `{` ` ` ` ` `// sorting the array in ` ` ` `// descending order` ` ` `Arrays.sort(a, Collections.reverseOrder());` ` ` ` ` `// store the final sum of ` ` ` `// all the rectangles area ` ` ` `// possible` ` ` `int` `sum = ` `0` `;` ` ` `boolean` `flag = ` `false` `;` ` ` ` ` `// temporary variable to ` ` ` `// store the length of rectangle` ` ` `int` `len = ` `0` `;` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{` ` ` ` ` `// Selecting the length of ` ` ` `// rectangle so that difference ` ` ` `// between any two number is 1 ` ` ` `// only. Here length is selected ` ` ` `// so flag is set` ` ` `if` `((a[i] == a[i + ` `1` `] || ` ` ` `a[i] - a[i+` `1` `] == ` `1` `) && ` ` ` `!flag) ` ` ` `{` ` ` `// flag is set means` ` ` `// we have got length of` ` ` `// rectangle` ` ` `flag = ` `true` `;` ` ` ` ` `// length is set to` ` ` `// a[i+1] so that if ` ` ` `// a[i] a[i+1] is less ` ` ` `// than by 1 then also` ` ` `// we have the correct ` ` ` `// choice for length` ` ` `len = a[i+` `1` `];` ` ` ` ` `// incrementing the counter ` ` ` `// one time more as we have ` ` ` `// considered a[i+1] element ` ` ` `// also so.` ` ` `i++;` ` ` `}` ` ` ` ` `// Selecting the width of rectangle` ` ` `// so that difference between any` ` ` `// two number is 1 only. Here width` ` ` `// is selected so now flag is again ` ` ` `// unset for next rectangle` ` ` `else` `if` `((a[i] == a[i + ` `1` `] || ` ` ` `a[i] - a[i+` `1` `] == ` `1` `) && ` ` ` `(flag)) ` ` ` `{` ` ` `// area is calculated for` ` ` `// rectangle` ` ` `sum = sum + a[i+` `1` `] * len;` ` ` ` ` `// flag is set false` ` ` `// for another rectangle` ` ` `// which we can get from` ` ` `// elements in array` ` ` `flag = ` `false` `;` ` ` ` ` `// incrementing the counter ` ` ` `// one time more as we have ` ` ` `// considered a[i+1] element ` ` ` `// also so.` ` ` `i++;` ` ` `}` ` ` `}` ` ` ` ` `return` `sum;` ` ` `}` ` ` ` ` `// Driver code` ` ` `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));` ` ` `}` `}` `// This code is contributed by ` `// Manish Shaw(manishshaw1)` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find sum ` `# of all area rectangle` `# possible` `# Function to find ` `# area of rectangles` `def` `MaxTotalRectangleArea(a, n) :` ` ` ` ` `# sorting the array in` ` ` `# descending order` ` ` `a.sort(reverse ` `=` `True` `)` ` ` `# store the final sum of` ` ` `# all the rectangles area` ` ` `# possible` ` ` `sum` `=` `0` ` ` `flag ` `=` `False` ` ` `# temporary variable to store` ` ` `# the length of rectangle` ` ` `len` `=` `0` ` ` `i ` `=` `0` ` ` ` ` `while` `(i < n` `-` `1` `) :` ` ` `if` `(i !` `=` `0` `) :` ` ` `i ` `=` `i ` `+` `1` ` ` ` ` `# Selecting the length of` ` ` `# rectangle so that difference ` ` ` `# between any two number is 1 ` ` ` `# only. Here length is selected` ` ` `# so flag is set` ` ` `if` `((a[i] ` `=` `=` `a[i ` `+` `1` `] ` `or` ` ` `a[i] ` `-` `a[i ` `+` `1` `] ` `=` `=` `1` `) ` ` ` `and` `flag ` `=` `=` `False` `) :` ` ` ` ` `# flag is set means` ` ` `# we have got length of` ` ` `# rectangle` ` ` `flag ` `=` `True` ` ` `# length is set to` ` ` `# a[i+1] so that if ` ` ` `# a[i+1] is less than a[i]` ` ` `# by 1 then also we have` ` ` `# the correct chice for length` ` ` `len` `=` `a[i ` `+` `1` `]` ` ` `# incrementing the counter ` ` ` `# one time more as we have ` ` ` `# considered a[i+1] element` ` ` `# also so.` ` ` `i ` `=` `i ` `+` `1` ` ` `# Selecting the width of rectangle` ` ` `# so that difference between any` ` ` `# two number is 1 only. Here width` ` ` `# is selected so now flag is again ` ` ` `# unset for next rectangle` ` ` `elif` `((a[i] ` `=` `=` `a[i ` `+` `1` `] ` `or` ` ` `a[i] ` `-` `a[i ` `+` `1` `] ` `=` `=` `1` `) ` ` ` `and` `flag ` `=` `=` `True` `) : ` ` ` ` ` `# area is calculated for` ` ` `# rectangle` ` ` `sum` `=` `sum` `+` `a[i ` `+` `1` `] ` `*` `len` ` ` ` ` `# flag is set false` ` ` `# for another rectangle` ` ` `# which we can get from` ` ` `# elements in array` ` ` `flag ` `=` `False` ` ` `# incrementing the counter ` ` ` `# one time more as we have ` ` ` `# considered a[i+1] element` ` ` `# also so.` ` ` `i ` `=` `i ` `+` `1` ` ` ` ` `return` `sum` `# Driver code` `a ` `=` `[ ` `10` `, ` `10` `, ` `10` `, ` `10` `, ` `11` `, ` `10` `,` ` ` `11` `, ` `10` `, ` `9` `, ` `9` `, ` `8` `, ` `8` `]` `n ` `=` `len` `(a)` `print` `(MaxTotalRectangleArea(a, n))` `# This code is contributed by` `# Manish Shaw (manishshaw1)` |

*chevron_right*

*filter_none*

## C#

`// C# code to find sum of all area rectangle` `// possible` `using` `System;` `class` `GFG {` ` ` `// Function to find ` ` ` `// area of rectangles` ` ` `static` `int` `MaxTotalRectangleArea(` `int` `[]a, ` ` ` `int` `n)` ` ` `{` ` ` ` ` `// sorting the array in descending` ` ` `// order` ` ` `Array.Sort(a);` ` ` `Array.Reverse(a);` ` ` ` ` `// store the final sum of all the` ` ` `// rectangles area possible` ` ` `int` `sum = 0;` ` ` `bool` `flag = ` `false` `;` ` ` ` ` `// temporary variable to store the ` ` ` `// length of rectangle` ` ` `int` `len =0;` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{` ` ` ` ` `// Selecting the length of ` ` ` `// rectangle so that difference ` ` ` `// between any two number is 1 ` ` ` `// only. Here length is selected ` ` ` `// so flag is set` ` ` `if` `((a[i] == a[i + 1] || a[i] - ` ` ` `a[i + 1] == 1) && (!flag)) ` ` ` `{` ` ` `// flag is set means` ` ` `// we have got length of` ` ` `// rectangle` ` ` `flag = ` `true` `;` ` ` ` ` `// length is set to` ` ` `// a[i+1] so that if ` ` ` `// a[i] a[i+1] is less ` ` ` `// than by 1 then also` ` ` `// we have the correct ` ` ` `// choice for length` ` ` `len = a[i + 1];` ` ` ` ` `// incrementing the counter ` ` ` `// one time more as we have ` ` ` `// considered a[i+1] element ` ` ` `// also so.` ` ` `i++;` ` ` `}` ` ` ` ` `// Selecting the width of rectangle` ` ` `// so that difference between any` ` ` `// two number is 1 only. Here width` ` ` `// is selected so now flag is again ` ` ` `// unset for next rectangle` ` ` `else` `if` `((a[i] == a[i + 1] || ` ` ` `a[i] - a[i + 1] == 1) && (flag)) ` ` ` `{` ` ` `// area is calculated for` ` ` `// rectangle` ` ` `sum = sum + a[i + 1] * len;` ` ` ` ` `// flag is set false` ` ` `// for another rectangle` ` ` `// which we can get from` ` ` `// elements in array` ` ` `flag = ` `false` `;` ` ` ` ` `// incrementing the counter ` ` ` `// one time more as we have ` ` ` `// considered a[i+1] element ` ` ` `// also so.` ` ` `i++;` ` ` `}` ` ` `}` ` ` ` ` `return` `sum;` ` ` `}` ` ` ` ` `// Driver code` ` ` `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));` ` ` `}` `}` `// This code is contributed by anuj_67.` |

*chevron_right*

*filter_none*

## PHP

`<?php` `// PHP code to find sum ` `// of all area rectangle` `// possible` `// Function to find ` `// area of rectangles` `function` `MaxTotalRectangleArea( ` `$a` `, ` `$n` `)` `{` ` ` `// sorting the array in` ` ` `// descending order` ` ` `rsort(` `$a` `);` ` ` `// store the final sum of` ` ` `// all the rectangles area` ` ` `// possible` ` ` `$sum` `= 0;` ` ` `$flag` `= false;` ` ` `// temporary variable to store` ` ` `// the length of rectangle` ` ` `$len` `;` ` ` ` ` `for` `( ` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{` ` ` ` ` `// Selecting the length of` ` ` `// rectangle so that difference ` ` ` `// between any two number is 1 ` ` ` `// only. Here length is selected` ` ` `// so flag is set` ` ` `if` `((` `$a` `[` `$i` `] == ` `$a` `[` `$i` `+ 1] ` `or` `$a` `[` `$i` `] - ` ` ` `$a` `[` `$i` `+ 1] == 1) ` `and` `(!` `$flag` `)) ` ` ` `{` ` ` `// flag is set means` ` ` `// we have got length of` ` ` `// rectangle` ` ` `$flag` `= true;` ` ` `// length is set to` ` ` `// a[i+1] so that if ` ` ` `// a[i+1] is less than a[i]` ` ` `// by 1 then also we have` ` ` `// the correct chice for length` ` ` `$len` `= ` `$a` `[` `$i` `+ 1];` ` ` `// incrementing the counter ` ` ` `// one time more as we have ` ` ` `// considered a[i+1] element` ` ` `// also so.` ` ` `$i` `++;` ` ` `}` ` ` `// Selecting the width of rectangle` ` ` `// so that difference between any` ` ` `// two number is 1 only. Here width` ` ` `// is selected so now flag is again ` ` ` `// unset for next rectangle` ` ` `else` `if` `((` `$a` `[` `$i` `] == ` `$a` `[` `$i` `+ 1] ` `or` ` ` `$a` `[` `$i` `] - ` `$a` `[` `$i` `+ 1] == 1) ` `and` ` ` `(` `$flag` `)) ` ` ` `{` ` ` `// area is calculated for` ` ` `// rectangle` ` ` `$sum` `= ` `$sum` `+ ` `$a` `[` `$i` `+ 1] * ` `$len` `;` ` ` `// flag is set false` ` ` `// for another rectangle` ` ` `// which we can get from` ` ` `// elements in array` ` ` `$flag` `= false;` ` ` `// incrementing the counter ` ` ` `// one time more as we have ` ` ` `// considered a[i+1] element` ` ` `// also so.` ` ` `$i` `++;` ` ` `}` ` ` `}` ` ` `return` `$sum` `;` `}` `// Driver code` `$a` `= ` `array` `( 10, 10, 10, 10,` ` ` `11, 10, 11, 10, ` ` ` `9, 9, 8, 8 );` `$n` `= ` `count` `(` `$a` `);` `echo` `MaxTotalRectangleArea(` `$a` `, ` `$n` `);` `//This code is contributed by anuj_67.` `?>` |

*chevron_right*

*filter_none*

**Output**

282

**Time Complexity:** O(nlog(n)) **Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Check if difference of areas of two squares is prime
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Find if two rectangles overlap
- Create a matrix with alternating rectangles of O and X
- Number of unique rectangles formed using N unit squares
- Number of rectangles in N*M grid
- Find all rectangles filled with 0
- Total area of two overlapping rectangles
- Number of rectangles in a circle of radius R
- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Smallest square formed with given rectangles
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Count the number of rectangles such that ratio of sides lies in the range [a,b]
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Find the minimum number of rectangles left after inserting one into another
- Count of distinct rectangles inscribed in an equilateral triangle
- Count Distinct Rectangles in N*N Chessboard

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.