Wrong output:
Question Link:
Solution Link:
Please check I'm getting wrong output
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.
- Mark the current element as next .
- If stack is not empty, compare top element of stack with next .
- If next is greater than the top element,Pop element from stack. next is the next greater element for the popped element.
- 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.