Practice

Two Sum II (Sorted Input)

Module 10 · Two Pointers

Problem

Given a 1-indexed, sorted array and a target, return the indices of the two numbers summing to target. Exactly one solution exists. Required: O(1) extra space (which rules out Module 6's hash map).

Examples

Example 1

Inputnumbers = [2,7,11,15], target = 9Output[1,2]

Example 2

Inputnumbers = [2,3,4], target = 6Output[1,3]

Example 3

Inputnumbers = [-1,0], target = -1Output[1,2]

Constraints

2 ≤ n ≤ 3·10⁴ · sorted non-decreasing · exactly one answer.

Attempt it first

The converging lesson used this exact problem to build the elimination argument — so the test here is whether you can reproduce the proof, not just the loop. Write the solution AND, in a comment, the one-sentence reason each pointer move is safe. Then compare.

Indices are 1-based, as the problem states — a common way to get this right in logic and wrong in output.