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 variable-size window. Expand right to include more elements and grow the running sum. Whenever the sum meets or exceeds the target, try shrinking from the left to find the smallest valid window, updating the minimum length each time.
Each element is added once (right pointer) and removed at most once (left pointer), so the total work is O(2n) = O(n). The inner while loop shrinks greedily, ensuring we find the smallest window that satisfies the sum condition.
Updated Feb 2026