Concept Flow - Next Greater Element Using Stack
Start with empty stack
Iterate over array from right to left
While stack not empty and top <= current element
Pop stack top
If stack empty -> No greater element, assign -1
Else top of stack is next greater element
Push current element onto stack
Move to next element left
Repeat until all elements processed
We scan the array from right to left, using a stack to keep track of candidates for the next greater element. We pop smaller or equal elements until we find a greater one or the stack is empty.
