Skip to content
DSA Patterns
Menu
Language

Pattern #02

Two Pointers

Essential

Sorted arrays, pair conditions, or coordinated movement from both ends.

When to use

Use on sorted arrays for pair/triple sums, or when you can decide which end to move based on a comparison. also for in-place reverse/partition.

Recognition cues

  • Array is sorted (or can be sorted without losing the answer)
  • Find pair / triple with a target sum
  • Opposite ends, meet in the middle
  • Remove duplicates in-place

Common pitfalls

  • Sorting when original indices are required (store indices first)
  • Infinite loops if you forget to move a pointer after a match
  • Duplicate handling in 3Sum-style problems

90-second recognition drill

Which pattern fits best?

  • Array is sorted (or can be sorted without losing the answer)
  • Find pair / triple with a target sum
  • Opposite ends, meet in the middle

Interactive

Mental model

A full worked walkthrough of the invariant. Pause, scrub the dots, or use ← →. Aim to narrate each step yourself.

Step 1 of 8
1/8

Sorted array. Find a pair that sums to target 9.

1
0
2
1
3
2
4
3
7
4
11
5
Statetarget=9L=0R=5

L = 0, R = n-1

Key:Active rangeLeft / slowRight / fastMatchSettledEliminated

How to think about it

Place left at the start and right at the end. Each step moves the pointer that can improve the answer. This path covers the opposite-ends family: a sorted pair, a frozen index plus a pair, the shorter wall, and the limiting rain wall. Same-direction slow/fast is the related in-place partition variant (remove duplicates); none of the problems below need it.

Template shapes

Shape Core move Example
Sorted pair A sum below the target raises lo. A sum above it lowers hi. Indexes are 1-based LC 167
Frozen index plus a pair Sort, freeze one index (two for 4Sum), pair on the suffix, and skip duplicates. LC 16 keeps the closest sum LC 15, LC 16, LC 18
Shorter wall Area is min(height[left], height[right]) * (right - left). Move the shorter side. Equal heights can move either LC 11
Limiting rain wall Advance the side with the smaller height. Water there is that side’s running max minus the height LC 42

Complexity baseline

O(n) after sorting (sort is often O(n log n)). Constant extra space unless you store results.

From template to problem

  1. Sort only when order does not matter. If the original indexes are required, store them first. LC 167 is already sorted.
  2. Move one pointer on every iteration. A match that leaves both pointers in place never ends.
  3. After a hit, skip equal values at the frozen index and at both pointers (LC 15, LC 18).
  4. For water, advance the lower side. Advancing the taller side uses the wrong limiting wall (LC 11, LC 42).

Template

Same skeleton in TypeScript, Python, and C#. Adapt the invariant; keep the structure.

Two Pointers · Template
/** Two pointers template: sorted two-sum (1-based indices). */
export function twoSumSorted(numbers: number[], target: number): number[] {
  let lo = 0, hi = numbers.length - 1;
  while (lo < hi) {
    const sum = numbers[lo]! + numbers[hi]!;
    if (sum === target) return [lo + 1, hi + 1];
    if (sum < target) lo++;
    else hi--;
  }
  throw new Error("No solution");
}
/** Two pointers template: sorted two-sum (1-based indices). */
export function twoSumSorted(numbers: number[], target: number): number[] {
  let lo = 0, hi = numbers.length - 1;
  while (lo < hi) {
    const sum = numbers[lo]! + numbers[hi]!;
    if (sum === target) return [lo + 1, hi + 1];
    if (sum < target) lo++;
    else hi--;
  }
  throw new Error("No solution");
}
#StatusProblemTypeDone
  1. 1#11 Container With Most WaterGuide
  2. 2#15 3SumGuide
  3. 3#16 3Sum ClosestGuide
  4. 4#18 4SumGuide
  5. 5#42 Trapping Rain WaterGuide
  6. 6#167 Two Sum II. Input Array Is SortedGuide