Two Sum II

Easy

Extra practice. This problem has no walkthrough slides. Try solving it with the pattern template on your own, and lean on the hints if you get stuck.

Question

Given a list of integers sorted in ascending order and a target value, return the indices of the two numbers that add up to the target.

You may assume exactly one valid pair exists unless stated otherwise below, and each input has at most one answer.

Input: nums = [1, 2, 3, 4, 6], target = 6

Output: [1, 3]

The numbers at indices 1 and 3 are 2 and 4, and 2 + 4 equals 6.

Input: nums = [2, 7, 11, 15], target = 9

Output: [0, 1]

Input: nums = [-3, -1, 0, 2, 5], target = 2

Output: [0, 4]

The numbers -3 and 5 add up to 2.

Input: nums = [1, 2, 3], target = 100

Output: []

No pair in the list adds up to 100.

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

Given nums = [1, 2, 4, 6, 10] and target = 8, which pair of indices does the two pointers approach return?
[0, 3]
[1, 3]
[0, 4]
[2, 3]

Take a moment to understand the problem and think of your approach before you start coding.