Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Use a monotonic decreasing stack of indices and iterate twice over the array using modulo arithmetic. The first pass pushes indices; both passes resolve next greater values by popping smaller elements.
Doubling traversal exposes wrap-around candidates while preserving nearest-right semantics. Each index is pushed once and popped once, so total work stays linear.
Updated Feb 2026