Concept Flow - Next Greater Element Using Stack
Start with empty stack
Iterate over array from left to right
While stack not empty and current element > top of stack element
Pop from stack and assign current element as next greater
Push current element index onto stack
Repeat until end of array
For remaining indices in stack, assign -1 as no next greater
Done
We scan the array left to right, using a stack to keep indexes of elements waiting for their next greater element. When a bigger element appears, we pop smaller ones and assign their next greater.
