The Two Pointer Technique in Arrays
Two pointers turns a nested loop into a single pass. Instead of trying every pair — O(n2) — you keep two indices and move exactly one of them per step, using a property of the data to decide which. On a sorted array that property is order, and it collapses the problem to O(n).
Converging pointers
Start one pointer at each end. Compare what they give you against the target, then move the pointer that can possibly improve the situation. Because the array is sorted, moving left right can only increase the sum, and moving right left can only decrease it — so each step eliminates a whole row of the pair matrix.
| Step | left | right | sum | Decision |
|---|---|---|---|---|
| 1 | 0 → 8 | 4 → 99 | 107 | 107 > 80, so the sum must shrink → move right inwards |
| 2 | 0 → 8 | 3 → 63 | 71 | 71 < 80, so the sum must grow → move left inwards |
| 3 | 1 → 17 | 3 → 63 | 80 | Match — return indices (1, 3) |
int[] twoSumSorted(int[] a, int target) {
int left = 0, right = a.length - 1;
while (left < right) {
int sum = a[left] + a[right];
if (sum == target) return new int[]{left, right};
if (sum < target) left++; // need a bigger sum
else right--; // need a smaller sum
}
return new int[]{-1, -1};
}
Each iteration moves one pointer inwards and they never cross back, so the loop runs at most n times: O(n) time, O(1) space, against O(n2) for the nested-loop version.
The fast / slow variant
The second flavour puts both pointers at the start and moves them at different speeds, or moves the trailing one only under a condition. This is the read/write pair from the previous lesson, and it is how in-place filtering works.
int removeDuplicates(int[] a) {
if (a.length == 0) return 0;
int slow = 0; // last unique element
for (int fast = 1; fast < a.length; fast++) {
if (a[fast] != a[slow]) {
a[++slow] = a[fast]; // keep the new value
}
}
return slow + 1; // new length
}When two pointers applies
- The array is sorted, or sorting it first does not break the question.
- You are looking for a pair or a triple that satisfies a comparison.
- You need to partition or filter in place with O(1) extra space.
- You are working from both ends — palindrome checks, container-with-most-water, reversal.
Moving both pointers at once
Advancing left and right in the same iteration skips candidate pairs and quietly produces wrong answers on some inputs. Exactly one pointer moves per step, and the comparison decides which.
Using the wrong loop condition
while (left < right) excludes pairing an element with itself. Use <= only when a single middle element is a legitimate answer, such as in binary search.
Key takeaways
- Two pointers replaces a nested loop with one pass when order lets you rule out candidates.
- Converging: one pointer at each end, move the one that can improve the result.
- Fast/slow: both start left, the trailing pointer marks where the next kept element goes.
- O(n) time, O(1) space — and the sort, if you add one, dominates at O(n log n).
Practice
| Problem | Pattern | Level |
|---|---|---|
| Two sum on a sorted array | Converging pointers | Easy |
| Valid palindrome, ignoring non-alphanumerics | Converging pointers | Easy |
| Container with most water | Converging pointers | Medium |
| Three sum | Sort + two pointers | Medium |
| Trapping rain water | Converging pointers | Hard |
Frequently asked questions
When should I use the two pointer technique?
When the array is sorted (or can be sorted) and you are searching for a pair, a triple, or a partition. The order lets each comparison eliminate many candidates at once instead of one at a time.
Does the array have to be sorted for two pointers?
For the converging variant, effectively yes — the decision about which pointer to move depends on order. The fast/slow variant works on unsorted arrays because it filters rather than compares against a target.
What is the difference between two pointers and sliding window?
A sliding window is a two-pointer variant where the pointers both move forward and the span between them is the answer. Converging two pointers move towards each other and the span shrinks.
Test yourself on Arrays
Reading is not recall. Take a timed quiz on this topic solo, or share a room code and battle friends on it.
⚡ Start the Arrays quiz