mediumArrayTwo PointersTwo Pointers

Two Sum II - Input Array Is Sorted

mediumTime: O(n)Space: O(1)

Recognize the pattern

sorted arrayfind pair with target sumtwo ends converge

Brute force idea

A straightforward first read of Two Sum II - Input Array Is Sorted is this: Check every pair with nested loops. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

A calmer way to see Two Sum II - Input Array Is Sorted is this: Two pointers at start and end: if sum too small move left right, too large move right left. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout Two Sum II - Input Array Is Sorted is this: Because the array is sorted, moving left pointer increases the sum and moving right decreases it. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

The trap in Two Sum II - Input Array Is Sorted usually looks like this: Returning values instead of 1-indexed positions — read the problem carefully. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Two Pointers Pattern