Two Pointers
Two pointers: walking a sorted array from both ends, or rewriting it in place with a read and a write pointer, and the precondition each needs.
Two pointers walk one array with two indices, either inward from both ends of sorted input or forward together as a read and a write pointer, so each step settles a value for good and the whole pass is O(n) with O(1) extra space. It matters because a pair search on sorted input, or an array rewritten in place, otherwise costs a nested loop or a second array. The topic opens on a pair found from both ends of a sorted array, removes zero readings from a log by copying each kept value once, and gives the opposite-ends template in Python, Java and TypeScript. It finds triples of weights that make a target without counting any twice, pairs riders on tandem bikes under a weight limit, shows a pair search walking past its answer on unsorted input, and ends with an exercise on closing the gaps in a row of parked cars.
Contents
8 cardsnot started