Two Pointers
01 / The shared mechanism
Two indices moving over the same sequence to maintain an invariant — the move rule changes per sub-pattern, but the mechanism is shared.
02 / The one-sentence essence
When the input is sorted and the answer is a pair satisfying a relation, place pointers at both ends and move whichever side is wrong — each move is forced by the comparison.
Problemfind i, j with arr[i] + arr[j] = targetInput[1, 2, 4, 7, 11, 15]target9
10
21
42
73
114
155
Sorted input, target 9. Place left at the start and right at the end.
step
0 / 8
L · R
—
arr[L] + arr[R]
—
vs target
—
0 / 8
03 / The pattern signature
# two indices over one sorted sequenceleft ← 0right ← n − 1while left < right:// compare and decide which side to moveif arr[left] + arr[right] = target:return (left, right)elif sum < target:left++ // need a larger sumelse:right-- // need a smaller sum
04 / When to recognize this pattern
"sorted"
The input is already sorted, or you can sort it cheaply. Sortedness is what makes each move definite.
"pair"
The answer involves exactly two elements — a sum, a difference, a match.
"palindrome / mirror"
The data has a symmetric structure to verify, like comparing characters from both ends inward.
"closest to"
You're minimizing distance rather than matching exactly — track the best pair as you converge.
05 / Common pitfalls
Reaching for it on unsorted data.
Without an ordering, moving a pointer doesn't monotonically change the candidate. Sort first, or this is the wrong tool.
Off-by-one on
while left < right.Use
< when a pair requires two distinct indices; use <= only when the same index can satisfy the relation alone (rare).Forgetting to advance past duplicates.
For problems like 3Sum, after a hit you may need to skip equal neighbors on both sides so the next iteration doesn't emit the same pair twice.
06 / Go practice — on LeetCode
easy15
01Palindrome Number— LC 9→02Remove Duplicates from Sorted Array— LC 26→03Remove Element— LC 27→04Merge Sorted Array— LC 88→05Valid Palindrome— LC 125→06Palindrome Linked List— LC 234→07Move Zeroes— LC 283→08Reverse String— LC 344→09Reverse Vowels of a String— LC 345→10Is Subsequence— LC 392→11Valid Palindrome II— LC 680→12Sort Array By Parity— LC 905→13Sort Array By Parity II— LC 922→14Squares of a Sorted Array— LC 977→15Two Sum Less Than K— LC 1099→
medium18
01Longest Palindromic Substring— LC 5→02Container With Most Water— LC 11→033Sum— LC 15→043Sum Closest— LC 16→054Sum— LC 18→06Sort Colors— LC 75→07Remove Duplicates from Sorted Array II— LC 80→08Two Sum II - Input Array Is Sorted— LC 167→093Sum Smaller— LC 259→10Longest Word in Dictionary through Deleting— LC 524→11K-diff Pairs in an Array— LC 532→12Shortest Unsorted Continuous Subarray— LC 581→13Valid Triangle Number— LC 611→14Advantage Shuffle— LC 870→153Sum With Multiplicity— LC 923→16Max Number of K-Sum Pairs— LC 1679→17Maximum Distance Between a Pair of Values— LC 1855→18Maximum Tastiness of Candy Basket— LC 2517→
— / When to use which
converging
Use when the array is sorted and the answer is a pair.
two-sum · palindrome · container
fast / slow
Use for in-place modification or cycle detection.
remove-dupes · linked-list cycles
sliding window
Use when the condition is about a contiguous chunk.
longest/shortest · at-most-k