Two pointers
Two pointers is a technique that walks a sequence with two indexes. They move toward each other, or in the same direction at different speeds, so you finish in one pass instead of nested loops.
Detailed Explanation
A nested loop over an array is O(n²). Many of those loops are asking a question that only needs each index to move forward. Two pointers keep that progress. Once left or right advances, it never goes back, so the body runs O(n) times.
The inward pair starts at 0 and n-1. You use it on a sorted array: two-sum of a pair, container-with-most-water, reversing in place. If the sum is too small, move the left pointer right. If it is too large, move the right pointer left. Because the array is sorted, that move cannot skip a useful pair you still need.
The same-direction pair is a slow pointer and a fast pointer, or a read pointer and a write pointer. Compact zeros, remove duplicates in place, and the cycle-finding trick on a linked list (Floyd) are this family. The write pointer only advances when you keep an element. The fast pointer on a list gains two nodes while the slow pointer gains one; if they meet, there is a cycle.
You reach for two pointers when the problem mentions a sorted array and a pair, when you must do an in-place filter, or when a list might contain a cycle. You keep a hash map when the array is unsorted and you need an arbitrary pair (the classic two-sum). You keep a sliding window when the region of interest is a contiguous segment whose width changes with a condition.
The cost of using two pointers on unsorted data is a wrong answer, not an error. Sort first only if the problem allows it (sorting loses original indexes unless you store them). The cost of moving the wrong pointer is a missed pair. State the invariant: everything before write is the answer so far; everything outside [left, right] is done.
Off-by-one lives here. Inclusive bounds need left <= right. A write index usually points at the next slot to fill. Write tests for empty arrays, one element, and all elements kept.
Code Examples
public class TwoSumSorted {
static boolean hasPair(int[] sorted, int target) {
int left = 0;
int right = sorted.length - 1;
while (left < right) {
int sum = sorted[left] + sorted[right];
if (sum == target) {
return true;
} else if (sum < target) {
left++; // need a larger number
} else {
right--; // need a smaller number
}
}
return false;
}
public static void main(String[] args) {
int[] xs = {1, 2, 4, 7, 11};
System.out.println(hasPair(xs, 9)); // prints: true (2+7)
System.out.println(hasPair(xs, 14)); // prints: false
}
}public class Compact {
static int keepPositive(int[] xs) {
int write = 0;
for (int read = 0; read < xs.length; read++) {
if (xs[read] > 0) {
xs[write++] = xs[read];
}
}
return write; // new logical length
}
public static void main(String[] args) {
int[] xs = {1, -2, 3, 0, 4};
int n = keepPositive(xs);
System.out.println(n); // prints: 3
System.out.println(xs[0] + "," + xs[1] + "," + xs[2]); // prints: 1,3,4
}
}Common Gotchas
The array must be sorted for the inward pair
on unsorted input the algorithm is silently wrong.
`left <= right` vs `left < right`
a pair uses two distinct indexes, so `<` is correct; a palindrome check may need `<=`.
Overflow on `sorted[left] + sorted[right]`
use `long` for the sum when values can exceed `int`.
In-place write pointers overwrite unread data
only do that when `write <= read`.