Skip to main content
Intermediate ~15 min read

Two-Pointer Technique

A single O(n) pass with two coordinated indices: converging pairs, fast/slow runs, and in-place array surgery.

Prerequisites: Arrays: Traversal, Insertion & Deletion, Big O Notation & Time Complexity

Many array problems start life as nested loops: "check every pair" is O(n²). The two-pointer technique replaces one of the loops with logic, using the ordering of the data to prove which pairs never need checking. Two indices move through the array in coordinated ways, and each pointer moves at most n steps, so the whole pass is O(n).

There are two classic shapes. Converging pointers start at opposite ends and move toward each other, discarding impossible pairs as they go. Fast/slow (or left/runner) pointers both start at the left; the runner scans ahead and the left pointer marks where results get written. The first shape finds pairs; the second reshapes arrays in place.

Pattern 1, converging pointers: sorted pair sum in O(n)

two-pointers-pair.js
// Find two indices in a SORTED array whose values sum to target.
function pairWithSum(sorted, target) {
  let left = 0, right = sorted.length - 1;
  while (left < right) {
    const sum = sorted[left] + sorted[right];
    if (sum === target) return [left, right];
    if (sum < target) left++;    // need a bigger value → move left up
    else right--;                // need a smaller value → pull right in
  }
  return null;
}
// O(n) time, O(1) space, versus O(n²) for checking every pair.

Pattern 2, fast/slow: remove duplicates in place

two-pointers-dedupe.js
// Given a sorted array, move each unique value to the front and
// return the count of uniques. Everything past that is scratch space.
function removeDuplicates(sorted) {
  if (sorted.length === 0) return 0;
  let write = 1;                        // slow: next slot to write
  for (let read = 1; read < sorted.length; read++) {   // fast: scanner
    if (sorted[read] !== sorted[write - 1]) {
      sorted[write] = sorted[read];     // keep it, compact to front
      write++;
    }
  }
  return write;
}
// [1,1,2,3,3] → [1,2,3,_,_], returns 3

How to recognize a two-pointer problem

  1. 1

    Pairs or regions

    The question involves pairs (sum, difference) or a boundary between two zones (keep/discard, evens/odds).

  2. 2

    Order gives an excuse to skip

    Sorted data lets you prove "this half can never work", which is exactly the excuse the second pointer needs to move.

  3. 3

    In-place constraint

    "O(1) extra space" is a loud hint: you'll write results back into the same array with a slow/fast pair.

  4. 4

    Each pointer moves one way

    Correct solutions move each pointer monotonically, that's what makes the whole pass O(n) and never stuck.

Practice

sorted = [1, 2, 4, 7, 11], target = 13. Trace the converging pointers: which pair is checked FIRST, and what happens?

Reveal explanation

Start (1, 11) = 12 < 13, so left++ (moving left up can only increase the sum). Next (2, 11) = 13 → found at indices [1, 4]. Moving right left would decrease the sum, the wrong direction.

Check your understanding

  1. 1.Why does two pointers beat the nested loop on SORTED pair-sum problems?
  2. 2.In the fast/slow deduplication pattern, what does the slow (write) pointer track?
  3. 3.A palindrome check ('racecar' reads the same reversed) maps naturally to which pattern?

Finished reading?

Mark it complete to earn +50 XP. Progress stays in your browser.

Keep going