Two Pointers Technique

A technique that moves two positions through the same data structure with a rule, often solving the problem in O(n) time.

Technique

Two pointers coordinate two positions in one pass

The two pointers technique puts two markers on the same array or list. At each step, you look at the values at those two positions and use a simple rule to decide which pointer should move next. Because each move cuts down the remaining work, the technique is often much faster than brute force.

How to read the demo
Follow the highlighted cells as the pointers move across the array.

The highlighted cells are the values the algorithm is looking at right now. After each step, the message below the row explains what was compared and why one pointer moved.

In Opposite Direction, the pointers start at the two ends and move inward. In Same Direction, the fast pointer scans ahead while the slow pointer marks the next useful position.

What to try in the demo
Step slowly and watch which pointer moves after each comparison.

In Opposite Direction, notice how the current sum tells you which side can be ignored next. One move makes the remaining search space smaller.

In Same Direction, notice that the fast pointer reads every value, but the slow pointer only moves when the next kept value should be written forward.

Watch two positions in the array move according to a simple rule, one step at a time.

Pattern: Two Sum (Sorted)Time Complexity: O(n)Space Complexity: O(1)
idx 0Left
1
idx 1
2
idx 2
4
idx 3
6
idx 4
8
idx 5Right
10

Initialize pointers at both ends. Sum: 1 + 10 = 11

1 / 6
Code Example
// Opposite Direction
while (left < right) {
  const sum = arr[left] + arr[right];
  if (sum === target) return [left, right];
  if (sum < target) left++;
  else right--;
}
Pattern Types

Opposite direction and same direction solve different jobs

Both approaches use two pointers, but they do different kinds of work. One usually narrows a search from both ends. The other usually scans forward while building or tracking the useful part of the array.

Opposite direction
Best when the values at the two ends tell you which pointer should move.

Common examples include pair-sum and palindrome-style problems.

Each comparison tells you which side cannot be part of the answer.

That is why the search space gets smaller after every move.

Same direction
Best when one pointer reads ahead and the other tracks where the next good value should go.

Common examples include removing duplicates and compacting data in place.

The fast pointer scans the array while the slow pointer tracks progress.

This keeps the work linear and uses only constant extra space.

AspectOpposite DirectionSame Direction
Pointer layoutOne pointer starts at the left end and one starts at the right end.Both pointers move forward through the same sequence.
Best fitSorted arrays, pair sums, and problems where moving inward shrinks the search space.Deduplication, partitioning, and problems where one pointer scans while the other tracks progress.
Main ideaUse the comparison result to decide which side should move next.Let one pointer explore while the other marks the next valid write position.
Runtime benefitOften reduces nested-loop thinking to one linear pass.Often compresses scanning and rewriting into one linear pass.
Key Takeaways

The short version

  • Think of the technique as two markers moving through the same array with a rule for what moves next.
  • Opposite direction pointers are useful when each comparison lets you ignore one side of the search space.
  • Same direction pointers are useful when one pointer reads ahead and the other tracks the result.
  • The pattern often replaces O(n^2) brute force with O(n) time and O(1) extra space.