Find greater element

I have done this simple iterate the linear array but when I saw the solution of coding block explanation little bit undigested

@khemchandrs
you can’t do this question using two nested loop. It might give TLE for big testcase.
Here you’ve to use stack. Iterate from right to left, for every index check whether stack is empty or not. If stack is empty that means there is no element in the right array which is greater than element on current index. Ans for that index is “-1” . If stack is not empty, you have to pop the top of stack until you find the greater element. If exist ans for current index is that number else stack become empty (ans is “-1”). Insert the current element and move left and do the same for rest of the array.

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.