arraysstacks
A monotonic stack solves next-greater queries
Anonymous
Scan left to right, keeping indices whose values are waiting for a larger value. When a new value is larger than the top value, it answers that index; pop until the top is at least as large, then push the current index. Each index enters and leaves once, so the scan takes linear time.
0