Next greater element to the right
For each number, find the first strictly greater number to its right. In 2, 2, 5 both twos lead to 5.
The stack holds indices waiting for answers. Their values may decrease or repeat from bottom to top.
While the current number exceeds the value on top, record the current index as the answer and pop the top. Then push the current index.
Each index enters once and leaves at most once. The scan takes O(n) time and O(n) space.