Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Closest Pair

โญโญโญ advancedโณ 30 min read๐Ÿ“ Lesson 179 of 281

Sort by x, split, solve halves, and sweep a y-sorted strip โ€” the strip scan is O(1) amortized per point.

Divide, conquer, and the strip

After solving both halves, only points within delta of the split line matter. Sorted by y, each point has at most 7 neighbors worth checking โ€” a packing argument, not luck. All distances stay SQUARED: comparisons exact, sqrt only at the very end (or never, if the answer is squared).

โšก Now practice

Ready to Code
Geometry SuiteHull areas at 2e5 points, integer point-in-polygon under load, and closest pair at 1e5 points.
3 challenges ยท ยท ~110 min