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
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
Result array filled with next greater elements
We scan the array from right to left, using a stack to keep track of candidates for the next greater element. For each element, we pop smaller or equal elements from the stack until we find a greater one or the stack is empty.
