ASAPUtils Logo ASAPUtils
Week 1

Two Sum II — Input Array Is Sorted

Two Sum II with two pointers in O(1) space, with C++ and JavaScript solutions, a visualizer, and the exchange argument that proves discarding an element is safe.

The problem

Given a 1-indexed array of integers already sorted in non-decreasing order, find the two numbers that add up to a target and return their 1-based indices. There is exactly one solution, and you may not use O(n) extra space.

numbers = [2, 7, 11, 15], target = 9 -> [1, 2]

Related Problems