HISTOGRAM AREA - STACK

https://ide.codingblocks.com/s/418759 ,
why my code is not running , i’d code this acc. to what bhaiya had explained.

Hello @CODER_JATIN, I have updated your code and probably it will work now.


The issue was you were writing
while(arr[s.top()] < arr[i] && !s.empty())
This is wrong as it might be possible we don’t have any element and in case of the empty stack it will give the error as instead of checking first that we have any element or not we are trying to access s.top() so change this condition to
while(!s.empty() && arr[s.top()] < arr[i])
And after this also write the while outside the for loop you were writing the condition inside the if block. There we need to use while loop to ensure we check all the elements.
I hope it will be clear to you. Pls, ask in case of any confusion.
Thanks :slight_smile:

thik hai bhaiya , matlab 2 galtiyan thi ek to while condition mai (!empty()) pehle aana chahiye or for loop khatam hone baad , saare elements jo reh gye unhe check krne ke liye bhi while loop hona chahiye?

and bhaiya , iski time and space complexity O(N) hi rhegi na?

Yes it will be O(n) both time and space complexity.

Okay , thank you ji :slight_smile:

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.