Pattern #02
Two Pointers
EssentialSorted arrays, pair conditions, or coordinated movement from both ends.
When to use
Use on sorted arrays for pair/triple sums, or when you can decide which end to move based on a comparison. also for in-place reverse/partition.
Recognition cues
- Array is sorted (or can be sorted without losing the answer)
- Find pair / triple with a target sum
- Opposite ends, meet in the middle
- Remove duplicates in-place
Common pitfalls
- Sorting when original indices are required (store indices first)
- Infinite loops if you forget to move a pointer after a match
- Duplicate handling in 3Sum-style problems
90-second recognition drill
Which pattern fits best?
- Array is sorted (or can be sorted without losing the answer)
- Find pair / triple with a target sum
- Opposite ends, meet in the middle
Interactive
Mental model
A full worked walkthrough of the invariant. Pause, scrub the dots, or use ← →. Aim to narrate each step yourself.
L = 0, R = n-1
How to think about it
Place left at the start and right at the end. Each step moves the pointer that can improve the answer. This path covers the opposite-ends family: a sorted pair, a frozen index plus a pair, the shorter wall, and the limiting rain wall. Same-direction slow/fast is the related in-place partition variant (remove duplicates); none of the problems below need it.
Template shapes
| Shape | Core move | Example |
|---|---|---|
| Sorted pair | A sum below the target raises lo. A sum above it lowers hi. Indexes are 1-based |
LC 167 |
| Frozen index plus a pair | Sort, freeze one index (two for 4Sum), pair on the suffix, and skip duplicates. LC 16 keeps the closest sum | LC 15, LC 16, LC 18 |
| Shorter wall | Area is min(height[left], height[right]) * (right - left). Move the shorter side. Equal heights can move either |
LC 11 |
| Limiting rain wall | Advance the side with the smaller height. Water there is that side’s running max minus the height | LC 42 |
Complexity baseline
O(n) after sorting (sort is often O(n log n)). Constant extra space unless you store results.
From template to problem
- Sort only when order does not matter. If the original indexes are required, store them first. LC 167 is already sorted.
- Move one pointer on every iteration. A match that leaves both pointers in place never ends.
- After a hit, skip equal values at the frozen index and at both pointers (LC 15, LC 18).
- For water, advance the lower side. Advancing the taller side uses the wrong limiting wall (LC 11, LC 42).
Template
Same skeleton in TypeScript, Python, and C#. Adapt the invariant; keep the structure.
/** Two pointers template: sorted two-sum (1-based indices). */
export function twoSumSorted(numbers: number[], target: number): number[] {
let lo = 0, hi = numbers.length - 1;
while (lo < hi) {
const sum = numbers[lo]! + numbers[hi]!;
if (sum === target) return [lo + 1, hi + 1];
if (sum < target) lo++;
else hi--;
}
throw new Error("No solution");
}
/** Two pointers template: sorted two-sum (1-based indices). */
export function twoSumSorted(numbers: number[], target: number): number[] {
let lo = 0, hi = numbers.length - 1;
while (lo < hi) {
const sum = numbers[lo]! + numbers[hi]!;
if (sum === target) return [lo + 1, hi + 1];
if (sum < target) lo++;
else hi--;
}
throw new Error("No solution");
}
- 1#11 Container With Most WaterGuidemedium
- 2#15 3SumGuidemedium
- 3#16 3Sum ClosestGuidemedium
- 4#18 4SumGuidemedium
- 5#42 Trapping Rain WaterGuidehard
- 6#167 Two Sum II. Input Array Is SortedGuidemedium