Skip to content
Decoder

Two Sum II (Sorted Input)

Medium

Problem

You are given a list numbers that is sorted in ascending order and a number target. Find the two different positions whose values add up to target and return their 0-based indices in increasing order.

Example 1

Input
numbers = [2, 7, 11, 15], target = 9
Output
[0, 1]

Example 2

Input
numbers = [2, 3, 4], target = 6
Output
[0, 2]

Example 3

Input
numbers = [-1, 0], target = -1
Output
[0, 1]

Constraints

  • 2 <= numbers.length <= 3 * 10^4
  • -1000 <= numbers[i] <= 1000
  • -1000 <= target <= 1000
  • Exactly one valid pair exists.
  • Use only O(1) extra space.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.