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
Process nums2 with a monotonic decreasing stack. When current value is greater than stack top, it becomes the next greater element for popped values. Store this in a map, then answer nums1 queries from the map.
The stack keeps values waiting for a larger element. The first value that pops them is their nearest greater element to the right.
Updated Feb 2026