What is wrong with this code, it shows segmentation error

please tell me the reason for the error, i am facing it since a long time, even after searching it online, the doubts aint clear

hi shivam

You are getting run time error because of this statement
while(currentp>=price[s.top()] and !s.empty()){
here if stack is empty it can’t access s.top();
correct statement will be
while(!s.empty() and currentp>=price[s.top()]){

here first we check whether stack is empty or not
if stack is empty it will not check other condition because in case of “and” if one condition is false output will be false irrespective of other

Modified Code

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.