Ram on his birthday ordered a cake which is in the shape of a histogram such that cake is divided into ‘N’ bars and each bar width equal to one. The height of each bar is known.
Since it’s his birthday, so he wants to eat the maximum of the cake, but he can have the cake only once and in rectangle shape. Help him find the area of the largest rectangle in the cake.
You will be given an array/list 'CAKE' of ‘N’ non-negative integers representing the cake's bar height where the width of each bar is one. You have to find the area of the largest rectangle in the cake.
Example:
For given array = {3,1,7,8,1,2}

Above is the cake in the shape of a histogram where the width of each bar is 1, given array cake = {3,1,7,8,1,2}, the largest rectangle is shown with red outline having an area of 7 * 2 = 14 units.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next 2 * 'T’ lines represent the ‘T’ test cases.
The first line of each test case contains a single integer 'N' denoting the size of the array.
The second line of each test case contains 'N' space-separated integers denoting the array elements.
Output format :
For each test case, return a single integer representing the largest area of the rectangle in the histogram.
Note:
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10^4
0 <= CAKE[I] <= 10^5
Where ‘CAKE[I]’ is the value at index 'i' of the array 'CAKE'.
Time limit: 1 sec
2
6
3 1 7 8 1 2
3
1 2 4
14
4
In test case 1, It is explained in the discussion, and the maximum area will be 14 units.
In test case 2, Array elements are 1,2,4. So the histogram will be:

And the maximum area is marked with red, and that is 2 * 2 = 4 units.
2
3
3 2 1
4
1 9 8 4
4
16
In test case 1, The array elements are 3,2 and 1, so the histogram will be:

We can clearly see that the rectangle with maximum area is marked with red having an area of 4 units.
In test case 2, Array elements are 1, 9, 8, 4. So the maximum area will be by bars with height 8 and 9, with total width of 3 and thus maximum area will be 8 * 2 = 16 units.
Try to consider all possible pairs.
The idea is to check all the possible combinations by taking every bar as a starting point of an area. So we will consider each bar one by one as the starting point, consider every bar is having an index greater than the index of the starting bar to calculate the area between them, and after calculating the area with every starting point, we update the maximum possible rectangular area and finally return it after all starting points are checked.
The steps are as follows:
O(N^2), Where ‘N’ denotes the number of elements in the Array.
Since we are using two nested loops of length N each and thus the time complexity will be O(N^2).
O(1)
Since we are using constant space and thus the space complexity will be O(1).