AlgoFlow
beginnerArrays & Two Pointers

Pair with target sum

Given a sorted array of integers and a target, return the indices of the two numbers that add up to the target. Exactly one solution exists and you may not use the same element twice.

Examples

input nums = [2, 7, 11, 15], target = 9

output [0, 1]

2 + 7 = 9, and both values sit at the front of the array.

input nums = [1, 3, 4, 6, 8, 11], target = 10

output [2, 4]

4 + 6 = 10. A nested loop would find it too — in O(n²).

Hints

Try the problem first. Hints are revealed one at a time so you keep thinking.

Solution

The written approach, complexity analysis and follow-ups stay hidden until you ask for them.