Every group of three takes 45 seconds.
transcript
Three loops are correct. They are also far too slow. Count the cost first. A music streaming app built this tool first.
A catalogue has three thousand tracks. An editor waits on screen. The limit is two seconds. Every possible block is wanted.
One loop picks the first track. A second loop picks the second. A third loop picks the third. Every group is checked once. It is correct. It is also cubic.
That is n cubed over six groups. For three thousand tracks, about four and a half billion. At a hundred million a second, forty-five seconds. The editor waits for two seconds only. That is more than twenty times too slow.
Fix one track, and search a pair. Under ten million steps instead. Same answer, far fewer checks. Sort a copy first, so the pointers can move by size.
Always count before you write the code. Full breakdown in the lecture.