Skip to content
Veltoryx Technologies
FreeProblem 17● MediumLeetCode #167 ↗3 min readUpdated 30 October 2026

Two Sum II - Input Array Is Sorted

Find the pair that adds up to a target in a sorted array by squeezing in from both ends, with O(1) extra space.

01The problem

numbers is sorted ascending. Find the two numbers that add up to target and return their positions counting from 1, as [index1, index2] with index1 < index2. Exactly one answer exists, and you must use O(1) extra space.

InputOutputWhy
[2, 7, 11, 15], target 9[1, 2]2 + 7
[2, 3, 4], target 6[1, 3]2 + 4

Constraints: 2 to 30,000 numbers, sorted ascending, exactly one solution.

02How to spot it

  • The input is sorted.
  • You need a pair with a sum.
  • Constant extra space is required.

So: squeeze from both ends.

03Pseudocode

text
l = 0 ; r = n - 1
while l < r:
  sum = a[l] + a[r]
  if sum == target: return [l+1, r+1]
  if sum < target: l += 1
  else: r -= 1
  1. Start with the smallest and the largest.
  2. Too small? Move the left finger right.
  3. Too big? Move the right finger left.

04Watch it work

Visualizer · step through the algorithmnumbers = [1,3,4,6,8,11], target = 10
10
31
42
63
84
115
lr

Start with the smallest and the largest. We want a pair that adds up to 10.

Pseudocode

  1. l = 0 ; r = n - 1
  2. while l < r:
  3. sum = a[l] + a[r]
  4. if sum == target: return [l+1, r+1]
  5. if sum < target: l += 1
  6. else: r -= 1

Variables

l
0
r
5
target
10
step 1 / 6

05Step-by-step dry run

  1. 1
    10
    31
    42
    63
    84
    115
    lr

    Start with the smallest and the largest. We want a pair that adds up to 10.

    l = 0r = 5target = 10

  2. 2
    10
    31
    42
    63
    84
    115
    lr

    1 + 11 = 12 is too big. Only a smaller right number can lower it: move r left.

    l = 0r = 5sum = 12

  3. 3
    10
    31
    42
    63
    84
    115
    lr

    1 + 8 = 9 is too small. Only a bigger left number can raise it: move l right.

    l = 0r = 4sum = 9

  4. 4
    10
    31
    42
    63
    84
    115
    lr

    3 + 8 = 11 is too big. Only a smaller right number can lower it: move r left.

    l = 1r = 4sum = 11

  5. 5
    10
    31
    42
    63
    84
    115
    lr

    3 + 6 = 9 is too small. Only a bigger left number can raise it: move l right.

    l = 1r = 3sum = 9

  6. 6
    10
    31
    42
    63
    84
    115
    lr

    4 + 6 = 10, the target. Return positions [3, 4] (counting from 1).

    l = 2r = 3sum = 10

06Java solution

TwoSumSorted.java
import java.util.Arrays;

public class TwoSumSorted {
    static int[] twoSum(int[] numbers, int target) {
        int l = 0, r = numbers.length - 1;
        while (l < r) {
            int sum = numbers[l] + numbers[r];
            if (sum == target) return new int[] {l + 1, r + 1}; // 1-indexed
            if (sum < target) l++; // need bigger: move left up
            else r--;              // need smaller: move right down
        }
        return new int[] {-1, -1}; // never reached: one answer exists
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(twoSum(new int[] {1, 3, 4, 6, 8, 11}, 10)));
        System.out.println(Arrays.toString(twoSum(new int[] {2, 7, 11, 15}, 9)));
        System.out.println(Arrays.toString(twoSum(new int[] {-1, 0}, -1)));
        System.out.println(Arrays.toString(twoSum(new int[] {1, 2, 3, 4, 4, 9}, 8)));
    }
}
Expected output
[3, 4]
[1, 2]
[1, 2]
[4, 5]

Tricky lines, decoded

  • l + 1, r + 1: the problem counts positions from 1. A classic off-by-one trap.
  • sum < target → l++: only increasing the smaller number can raise the sum.

07Why it works

If a[l] + a[r] is too small, a[l] can't be in the answer with any element left of r either, because those are even smaller, so discarding l is safe. The same logic discards r when the sum is too big. Each step removes one impossible candidate, so the answer is never skipped.

Invariant: if an answer exists, both of its indices lie in [l, r].

08Time and space

  • Time: O(n). Each step moves one pointer.
  • Space: O(1). A hash map also gives O(n) time but uses O(n) memory.

09Edge cases

InputOutputWhy it matters
[-1, 0], -1[1, 2]negatives
[2, 3, 4], 6[1, 3]pair at the ends
[1, 2, 3, 4, 4, 9], 8[4, 5]equal values
[-3, -1, 0, 2, 5], -4[1, 2]all on the left
[5, 25, 75], 100[2, 3]pair at the right

10Common mistakes

  • Returning 0-based indices. The judge expects positions from 1.
  • Using a HashMap. It works, but ignores the sorted hint and the O(1) space rule.
  • l <= r. An element can't pair with itself: use l < r.

11Other approaches

ApproachTimeSpaceUse when
All pairsO(n²)O(1)Never
Two pointersO(n)O(1)Default for sorted input
Binary search per lO(n log n)O(1)Explaining alternatives
Hash mapO(n)O(n)Unsorted input (LC 1)

12In the interview

  • Count all pairs with sum < target? When a[l] + a[r] < target, all r − l pairs starting at l count: add them at once, then l++.
  • Three numbers? 3Sum (problem 24): fix one, run this exact loop on the rest.

13Check your understanding

Quiz · 2 questions

0 of 2 answered

  1. 1. The sum is too small. Which pointer moves?

  2. 2. Why l < r and not l <= r?

14Solve it yourself

Try it on LeetCode #167.