DSA Tracker

Medium

Two Sum II - Sorted Array

A medium Two Pointers problem included in Apna College, Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Two Pointers
Sheets
3
Core for
10 roles
Platform
LeetCode

The problem

Given a sorted array of integers, find two numbers that add up to a specific target and return their 1-indexed positions.

Example 1

Input
numbers = [2, 7, 11, 15], target = 9
Output
[1, 2]
Why
2 + 7 = 9, so the positions are 1 and 2.

Example 2

Input
numbers = [2, 3, 4], target = 6
Output
[1, 3]
Why
2 + 4 = 6.

Constraints

  • 2 <= numbers.length <= 3 * 10^4
  • -10^3 <= numbers[i] <= 10^3
  • numbers is sorted in non-decreasing order
  • -10^3 <= target <= 10^3

How to think about it

Updated 2026-09-09

Because the sequence is already ordered, pairing the smallest available number with the largest reveals everything about the sum. If that total is too big, no other partner can save the large value, so it can be discarded permanently. Every comparison safely shrinks the candidate range from one side.

Approaches, worst first

  1. All pairs scan

    time O(n^2) · space O(1)

    Test every pair of indices with nested loops. It ignores the sorted order completely and re-evaluates sums that monotonicity already proved impossible.

  2. Binary search complement

    time O(n log n) · space O(1)

    Iterate through each number and binary search for `target - numbers[i]` in the remaining suffix. Exploits sorting to drop one factor of n, but performs independent logarithmic lookups instead of preserving pointer momentum.

  3. Inward shrinking pointersWrite this one

    time O(n) · space O(1)

    Place pointers at index 0 and n-1. If their sum exceeds target, decrement right; if below target, increment left. Each step eliminates one row or column from the pair matrix, guaranteeing termination in a single sweep.

Where people lose marks · 3
  • Returning 0-based indices. The problem demands 1-indexed output, causing off-by-one failures despite finding the right values.
  • Using the same element twice. Pointers must meet the strict inequality `left < right`, preventing numbers[left] from doubling when target is 2 × numbers[left].
  • Overcomplicating the search with auxiliary hash maps. Storing elements incurs O(n) space on an array whose existing sorted invariant already solves it in O(1) space.

The theory behind it

Two Pointers — the ground this problem stands on. All Two Pointers problems

What Two Pointers is

Two pointers is a search coordinated by two fingers placed on different entries of an ordered row, stepping inward toward each other or advancing at different speeds. Instead of scanning with nested loops that re-examine identical pairs, each pointer moves unidirectionally based on comparisons. Because every movement permanently prunes bad candidate combinations, the search inspects the collection in a single joint pass.

When to reach for it

Reach for two pointers when problems require finding index pairs, reversing sequences, trapping rainwater, or filtering duplicates in sorted arrays. When an input is already sorted and an exhaustive search takes quadratic time, opposing pointers meeting in the center often solve it linearly. Fast and slow pointer variations also detect cycles or locate midpoint nodes across linked sequences.

How the pattern works

Initialize pointers at opposing edges for convergent searches, or start both at the beginning to move at different speeds. Under a convergent setup, calculate current pair metrics; if the sum falls short of your target, advance the lower pointer rightward to increase value, and if it exceeds your target, shift the upper pointer leftward to decrease value. Each pointer move throws away only pairs that were already known to be wrong, so the right answer is never missed.

What each operation costs

OperationTime
converging scan across sorted collectionO(n)
slow and fast cycle traversalO(n)
auxiliary memory overhead for pointer markersO(1)
What usually goes wrong with Two Pointers
  • Applying convergent opposite-end pointers to an unsorted collection without realizing that sorting was a necessary precondition for direction-based pruning.
  • Allowing pointers to cross past one another or crash into identical indices when matching duplicate values, skipping the termination condition.
  • Forgetting to advance pointers past repeated values during duplicate-skipping loops, creating unexpected infinite iterations on matching elements.

Which roles need this problem

Two Pointers is a core topic for these 10 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 15 more roles, including SDE / Backend Engineer, ML Engineer, Graphics Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Two Pointers problems

Problem set and role mapping as of .