Stock span problem complexity

How is the complexity O(n), when we have another while loop inside the for loop ? Should not the complexity be slightly more than O(n) ?

Hey shubham, it is O(n) only.Yes it seems more than O(n) at first look but if look closely you will observe that every element of array is added and removed from stack at most once.
So there are total 2n operations at most. Assuming that a stack operation (push or pop) takes O(1) time.
time complexity is O(n).

hey Shubham, if your query is resolved please mark this doubt as resolved. If not, free to ask here

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.