Fix all but two
transcript
Three items must follow one rule. Testing every triple costs n cubed. One idea cuts it to n squared. Sort the data, and fix all but two items.
Sometimes the first entry is fixed, as in three-sum. Sometimes the middle one is fixed. For triangles, the longest side is fixed.
Fix seven, the longest. Pointers sit at two and six. Two plus six beats seven. Three triangles at once. Move the high pointer down. Now it falls short. Six and four add two more. Five in all.
A thousand segments make over a hundred million triples to test. The sweep makes under five hundred thousand steps. That is the whole saving.
Sort first, so that order means something. Each pointer moves the total in one direction. So each step rules out a whole block of pairs.
Fix all but two items. Sort, then sweep the pair. Each step rules out a block of pairs. Full breakdown in the lecture.