I was trying to decode the logic in SPOJ HISTOGRA problem but couldn't think of any good solution. So, I referred this link. Now, I do get the logic behind this problem but now I am stuck at one part.
Here's my understanding:
1. We traverse the array from left to right
2. If the stack is empty or if a[i+1]th element is greater than a[i] then we push
3. If the next element is smaller than we pop and then we calculate the popped element's area.
Now, I am stuck at the step after this. I don't seem to understand how to calculate the area of the popped element. I mean, with respect to which elements should I be calculating the area?
The link I'm referring to says it should be asked 24 Oct '17, 22:09

The basic idea for this question is to find the next and previous smallest element for each of the elements of the array. This can be done in O(n) using stack. Now for each element a[i] you can find the ends of the histogram of height a[i] and which passes through i. If you want link to my solution. answered 25 Oct '17, 00:09
