Histogram Area , it is showing run time error idk why here is my ide for this code https://ide.codingblocks.com/s/96717

idk why it s showing run time error

Remove both “break” statements inside the function you have implemented.

Is there any need of these break statements? Think.
Won’t they break the processing of the unprocessed elements of the array if somewhere in between the stack becomes empty.

BTW this would correct the output/logic but your program will still cause TLE for some test cases.

Let’s understand the reason for this behavior with an example:
3
2 1 2

  1. 2 is pushed into the stack before the loop.
  2. (while loop )As 1 is smaller that the top of stack i.e. 2 so, you will pop the stack
  3. Here comes the problem. Now, the stack is empty. But you are checking if arr[s.top()]>= 1.
    What kind of index is that… Coz stack is empty.
    This is causing compile time error.

Solution:
Modify one statement with:
while(!s.empty() && arr[s.top()] >= currHeight) {}

Explanation:
Now before checking for the error causing condition, the compiler will check for !s.empty().
This will make the entire condition false for empty stack and compiler won’t execute the second condition i.e. the one causing error.
Hence, just modified the order of condition checks.

Hope, this would help.
If you still have doubts, feel free to ask.
And If you are satisfied, give a like.

I am marking this doubt as resolved as you have not replied to this thread for 9 days.
In case, you still have some doubts regarding the same problem, you can reopen this doubt again.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.