
The Two Pointers technique is one of the most versatile patterns in algorithm design.
Instead of scanning an array with nested loops (which often leads to an inefficient approach),
we place two indices on strategic positions and move them intelligently in order to solve the problem in linear time .
At its core, the idea is simple:
array = [ 2, 7, 11, 15 ]
^ ^
L R
Here, L and R are our two pointers, starting at the leftmost and rightmost ends of the array.
By eliminating unnecessary comparisons and shrinking the search space step by step, Two Pointers often transforms a brute-force
solution into an elegant and efficient one.
Many problems boil down to comparing values that lie at different positions in an array.
Instead of checking every pair, we can often infer where to move next:
This directional movement gives us efficiency: becomes time and space in most cases. Two Pointers applies to a wide range of real problems:
A special case of the two pointers technique is the Sliding Window, where both pointers move in the same direction while maintaining a window over the data (e.g., longest substring, max sum subarray).
It’s closely related, but deserves its own dedicated article, so we'll cover it separately.
Although the name “Two Pointers” looks generic, most problems fall into two major categories.
Both pointers start at opposite ends of a structure (usually a sorted array) and move toward each other.
array = [ 2, 7, 11, 15 ]
^ ^
L R
At each step, you compare values at L and R and decide which pointer to move.
This variant is ideal when:
Both pointers move in the same direction, but at different speeds.
array = [ 2, 7, 11, 15 ]
^ ^
S F
Use cases:
A famous example is Floyd’s Cycle Finding Algorithm (Tortoise and Hare):
slow moves 1 step at a timefast moves 2 steps at a timeThis trick solves linked-list cycle problems in O(n) time and O(1) extra space.
Here below you can find the most common patterns that utilize the Two Pointers technique.
When an array is sorted, we can efficiently search for pairs or shrink the search space by placing one pointer on the left and one on the right.
Main use cases
let L = 0
let R = n - 1
while (L < R) {
if (nums[L] + nums[R] == target) { /* ✅ found */ }
if (nums[L] + nums[R] < target) {
L++
} else {
R--
}
}
Some problems do not ask for an exact pair but for the best possible pair:
A classical example is the “Container With Most Water” problem: two pointers start at the edges and move toward the center while tracking the best possible area.
In this case/technique, always move the pointer with the smaller height, because moving the taller one cannot improve the area.
A natural extension of two pointers is to introduce a pivot and then apply the two-pointer logic on the remaining sub-array.
Used in problems like:
A typical structure:
for (let i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] === nums[i - 1]) {
continue; // skip pivot duplicates
}
let L = i + 1;
let R = nums.length - 1;
while (L < R) {
const sum = nums[i] + nums[L] + nums[R];
if (sum === 0) {
// found result
L++;
R--;
// skip duplicates on L
while (L < R && nums[L] === nums[L - 1]) L++;
// skip duplicates on R
while (L < R && nums[R] === nums[R + 1]) R--;
} else if (sum < 0) {
L++; // we need a bigger sum
} else {
R--; // we need a smaller sum
}
}
}
In this pattern, both pointers move toward the same direction, but at different speeds:
Used for:
The most famous implementation is Floyd’s Cycle Finding Algorithm (already mentioned above).
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next; // +1 step
fast = fast.next.next; // +2 steps
if (slow === fast) {
// ✅ cycle detected
break;
}
}
| Exercise | Technique | Solution |
|---|---|---|
| Merge Sorted Array | Opposite-direction Two Pointers | Solution |
| Two Sum II - Input Array Is Sorted | Opposite-direction Two Pointers | Solution |
| Container With Most Water | Opposite-direction Two Pointers / Optimal Boundaries | Solution |
| 3Sum | Three Pointers / Deduplication | Solution |
| Trapping Rain Water | Two Pointers / Boundary Scanning | Solution |