Decreasing monotonic stack: find the next greater element

Scan the array left to right. Before pushing each value, pop every stack value that is it, so the stack stays decreasing from bottom to top. Each pop resolves a next-greater-element: the value arriving is the first larger value to the right of the one it pops.

Goal For each value in the array, find its next greater element — the first larger value to its right.
Input array (scanned left → right)
Stack (values, decreasing top → bottom)
↓ bottom
current element in stack just popped