Next Greater Element in stack

one test case is not passing

Hello @nishiumrao please wait i am cheking your code.

@nishiumrao the problem in your code is this you are not checking for the circular way also and that is what you are not able to pass one test case.
like if the fist element is greater then the last elemen then the next greater for the last element will be first element.
moreover:
dry run your code for this test case:
4
8 2 3 6
correct output: -1 3 6 8.
your logic is correct but your implementation is not that correct. you should do this like in the manner i am sharing the corrected code you can see in that:
you should push index in the stack instead of elements that will also be very easy as you can handle their index as well as the element.


if you have any doubt you can ask here

Happy Learning!!

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.