Please check I'm getting wrong output

Wrong output:
Question Link:


Solution Link:

please go through the optimised algo discussed at gfg

  • Push the first element to stack.
  • Pick rest of the elements one by one and follow the following steps in loop.
    1. Mark the current element as next .
    2. If stack is not empty, compare top element of stack with next .
    3. If next is greater than the top element,Pop element from stack. next is the next greater element for the popped element.
    4. Keep popping from the stack while the popped element is smaller than next . next becomes the next greater element for all such popped elements
  • Finally, push the next in the stack.
  • After the loop in step 2 is over, pop all the elements from stack and print -1 as next element for them.

this is typical question

in case u are trying something from ur end
please explain ur logic for the same

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.