AlgoMaster Logo

Two Sum II - Input Array Is Sorted

Last Updated: April 2, 2026

medium
4 min read

Understanding the Problem

We have a sorted array and need to find two numbers that add up to a given target. The key constraints that shape our approach: the array is sorted, we must return 1-indexed positions, and we can only use constant extra space.

That last constraint is the interesting one. It rules out the classic hash map approach from the original Two Sum. The problem is telling us to use the sorted property to our advantage rather than relying on extra data structures.

Since the array is sorted and there's guaranteed to be exactly one solution, we can leverage the ordering to efficiently narrow down where the pair must be.

Key Constraints:

  • numbers.length up to 3 * 10^4 → O(n^2) brute force would be about 9 * 10^8 operations, which is borderline too slow. We should aim for O(n log n) or O(n).
  • The array is sorted → This is the biggest hint. Sorting enables binary search and two pointer techniques.
  • Constant extra space required → Hash map approach is technically off-limits (even though it would work on LeetCode).
  • Exactly one solution exists → No need to handle "no solution" cases.

Approach 1: Brute Force

Intuition

The most straightforward approach: try every pair of elements and check if they add up to the target. For each element at index i, check every element at index j > i. If their sum equals the target, return the 1-indexed positions.

This doesn't use the sorted property at all, which is a red flag that we're leaving performance on the table. But it's a valid starting point for building intuition.

Algorithm

  1. Loop through each index i from 0 to n-2.
  2. For each i, loop through each index j from i+1 to n-1.
  3. If numbers[i] + numbers[j] equals the target, return [i+1, j+1].
  4. Since a solution is guaranteed, we'll always find it before exhausting all pairs.

Example Walkthrough

1Start: try all pairs, i=0, j=1
0
i
2
1
7
j
2
11
3
15
1/3

Code

The brute force completely ignores the sorted property. Since the array is sorted, what if we used binary search to jump straight to where the complement would be, instead of scanning linearly?

Approach 2: Binary Search

Intuition

Since the array is sorted, we can do something smarter for each element. Instead of linearly scanning for its complement, we can binary search for it. For each numbers[i], the complement we need is target - numbers[i]. A binary search on the remaining portion of the array finds this complement in O(log n) instead of O(n).

This is a natural application of the sorted property. Whenever you see a sorted array and need to find a specific value, binary search should come to mind.

Algorithm

  1. For each index i from 0 to n-2:
    • Compute the complement: complement = target - numbers[i].
    • Binary search for complement in the subarray from index i+1 to n-1.
    • If found at index j, return [i+1, j+1].

Example Walkthrough

1i=0, nums[0]=2, complement = 9-2 = 7
0
i
2
1
7
2
11
3
15
1/4

Code

Binary search cut the inner loop from O(n) to O(log n), but we're still treating each element independently. What if both pointers could move simultaneously, each step eliminating a candidate and making progress toward the answer?

Approach 3: Two Pointers (Optimal)

Intuition

Here's the key insight: place one pointer at the beginning (smallest element) and one at the end (largest element). Their sum gives us information about which direction to move.

  • If the sum is too small, the left element is too small. Moving the left pointer right increases the sum.
  • If the sum is too large, the right element is too large. Moving the right pointer left decreases the sum.
  • If the sum equals the target, we found our pair.

This works because the array is sorted. Each pointer move eliminates exactly one candidate from consideration, and we never need to backtrack. The two pointers converge toward each other, and since a solution is guaranteed, they'll meet at the answer.

Algorithm

  1. Initialize left = 0 and right = n - 1.
  2. While left < right:
    • Compute sum = numbers[left] + numbers[right].
    • If sum == target, return [left + 1, right + 1].
    • If sum < target, increment left (we need a larger sum).
    • If sum > target, decrement right (we need a smaller sum).
  3. Return the result (guaranteed to be found in the loop).

Example Walkthrough

1Init: left=0, right=3
0
L
2
1
7
2
11
3
15
R
1/5

Code