Elements in the array are always in increasing order?
If not then how the stack approach will be able to give the next greater element of A[i] ?
Find the greater element
Hey @dishask99 stack will store all those elements whose next greater element is not known. so if we are at index i then we will compare the element at index i with the element at the top of stack if the element at top of stack is smaller than the current element than it means that we have found the greater element for the element at the top of stack we will keep doing this till the i’th index element is greater than the top of stack and then we will insert the element in stack and iterate forward.
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.