Can you tell what is wrong in my code and as its just passing one test case? Also can you tell a better approach for this problem?

Hey @isingh check for test case,
5
6 8 0 1 3
Expected output is: 8 -1 1 3 6
Yours is giving is: 8 32766 1 3 6
also you can do this problem in O(N) time complexity. It can be done using stack.
I am attaching a editorial so that you can read and get to know how this problem runs (Click here for editorial), also here’s the dry run by me which you can see to get an idea, though if you need you can also ask for that.

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.