

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.
For each test case, return a single integer representing the largest area of the rectangle in the histogram.
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
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:
As in the previous approach, we have seen that calculating the maximum area of the rectangle in any possible combination depends on the bar with smaller height but considering every combination was costing us more time complexity so to reduce this, we will be using stack. Now the question arises what we will store in the stack so the answer to this we will be storing the indices of the elements in decreasing order of heights that means we will be storing indexes of smaller height on top and of bigger heights below, therefore element on the top, index of the nearest and smallest element to the left of it will be below it and for right it will be the next element in the iteration. So will calculate the area with the help of this top element now.
The steps are as follows: