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.
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.
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.
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.
Initialize pointers at both ends. Sum: 1 + 10 = 11
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--;
}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.
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.
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.
| Aspect | Opposite Direction | Same Direction |
|---|---|---|
| Pointer layout | One pointer starts at the left end and one starts at the right end. | Both pointers move forward through the same sequence. |
| Best fit | Sorted 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 idea | Use the comparison result to decide which side should move next. | Let one pointer explore while the other marks the next valid write position. |
| Runtime benefit | Often reduces nested-loop thinking to one linear pass. | Often compresses scanning and rewriting into one linear pass. |
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.