#167

Two Sum II - Input Array Is Sorted

Medium Opposite ends

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

In a sorted (1-indexed) array, find the two positions whose values add up to target. Use only constant extra space.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: two numbers in a sorted array that add up to target.

Why this works: start with smallest + largest. Too small? Only moving left up can help. Too big? Only moving right down can help.

Example: [2, 7, 11, 15], target 9

2 + 15 = 17 too big → r--2 + 11 = 13 too big → r--2 + 7 = 9 ✓ → [1, 2]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Returning 0-based indexes — this problem wants 1-based ones.