Skip to content

Two Sum II - Input Array Is Sorted

Find the two numbers in a sorted array that add up to a target, with two pointers and O(1) extra space.

MediumTwo Pointers
Solve on LeetCode

The problem

You get an array numbers sorted in non-decreasing order and an integer target. Exactly one pair of different positions holds two numbers that add up to target. Return their positions as [index1, index2], counting from 1, with index1 < index2. Use only O(1) extra space.

Input numbers = [1, 4, 6, 9, 13], target = 15

Output [3, 4]

6 is the 3rd number and 9 is the 4th, and 6 + 9 = 15.

Input numbers = [-5, -5, 0, 2], target = -10

Output [1, 2]

The two -5s sit at different positions, so together they form the pair.

Key constraint: Only O(1) extra space is allowed, so the hash map from Two Sum is off the table.

How to spot the pattern

  • The array is sorted and you need a pair with a given sum.
  • Moving one end inward changes the sum in a predictable direction.

The idea

Start with the smallest and the largest number. If their sum is too small, the only way to grow it is to move the left pointer to a bigger number; if it is too big, move the right pointer to a smaller one. Each move throws away a number that can't be part of any valid pair, so the pointers close in on the answer in a single pass.

Brute force: Check every pair with two loops, which takes O(n^2) time; a hash map brings it to O(n) but uses O(n) space.

Step by step

  1. 1Set left = 0 and right = n - 1.
  2. 2Compute sum = numbers[left] + numbers[right].
  3. 3If sum equals target, return [left + 1, right + 1].
  4. 4If sum is smaller, move left one step right; if it is bigger, move right one step left.
  5. 5Repeat while left < right.

See the pattern move

The two pointers pattern on a small example. Step through it, or press play.

Code

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        left, right = 0, len(numbers) - 1
        while left < right:
            total = numbers[left] + numbers[right]
            if total == target:
                return [left + 1, right + 1]  # positions count from 1
            if total < target:
                left += 1  # need a bigger sum
            else:
                right -= 1  # need a smaller sum
        return []

Complexity

Time

O(n)

Space

O(1)

Every step moves one pointer inward, so there are at most n - 1 steps, and only two indices are stored.

Watch out for

  • Returning 0-based indices: this problem counts positions from 1.
  • Moving both pointers after a single comparison, which can step right past the answer.

Follow-up questions

Why is it safe to drop numbers[left] when the sum is too small?

Because numbers[right] is the largest number still in play. If even that partner gives a sum below the target, every other partner would too, so numbers[left] can't be in the answer. The same argument in reverse drops numbers[right] when the sum is too big.

How does this help with 3Sum?

Sort the array, fix one number, and run this two-pointer search on the numbers after it, looking for a sum of minus that number. That gives an O(n^2) answer to 3Sum.

In Hinglish

Sum chhota hai toh left aage badhao, bada hai toh right peeche lao; sorted array mein yahi sahi direction hai.

Related problems