Query regarding min and max stack

Why are we pushing the same data in min and max stack if the incoming data isn’t lesser or greater respectively than the top element?

hello @sukritbansal01

we are not pushing same data. first we are comparing it with previous min and max and then updating previous min/max if required.
and then we r pushing the new min /max in the stack

I’m asking if new min/max is same as old min/max, then why are we pushing it again?

to maintain all three vectors of equal size it is required.
otherwise size will be diffent.
and during pop operation it may give segfault

What if while popping, we pop from stack only and not min and max because we want element of stack only? Then can we prevent pushing same min/max?

no that will produce wrong result->
for example->
stack ->3 1
min stack -> 3 1
max stack -> 3 3
after 1 pop operation (only on stack)->
stack ->3
min stack -> 3 1
max stack -> 3 3
clearly the result contained by min stack is false. it should be 3 but it is gicing 1

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.