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
Return result array
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.
