Why should we use a stack on this?

A simple array could do the work. with O(n) time.
Why do we need a stack here?

Hi @Jo_Wang ,

Yes, you are right that this problem can be solved with the array in O(n) but the requirement says we have to implement it with the stack. Just like all of the problems of lo bop can be done with recursion but when we are told to do a specific task using recursion then we can’t use a loop in that particular scenario.

I hope I have answered your question and please feel free to ask anything.