Two Sum II (Sorted)

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Two Sum II (Sorted)

Approach

Place one pointer at the start and one at the end of the sorted array. If the sum is too small, advance the left pointer to increase it. If too large, retreat the right pointer to decrease it. This converges on the target pair in one pass.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Two Pointers (Converging)

Why It Works

Sorted order guarantees that moving the left pointer right increases the sum and moving the right pointer left decreases it, so each step provably eliminates at least one candidate pair.

Updated Feb 2026