Step 2/3 Iterate through the rest of the elements in a for-loop.
- Mark the iterated on/current element as the
nextelement. - If stack is not empty, compare
topelement of the stack withnext. - If
nextis greater than the top element,popthe element from the stack. What this means is that we've determinednextis the next greater element for the popped number. - While the popped element is smaller than
next, keep popping off the top of the stack. As the logic follows,nextbecomes the next greater element for all the popped elements.


