One pair, many selections
transcript
A streaming app fills a slot with a block of tracks. The shortest and longest track must fit together. Count the blocks without listing them.
Order inside a block does not matter, so sort. Then the smallest track is at the left. And the largest track is at the right.
The tracks three and six fit the slot. The tracks between them may join or not. That makes four selections starting with three.
Listing every selection is far too slow. Add two to the power of the gap instead. Then move the low pointer in.
Powers of two grow very fast. Keep every power modulo a prime. The prime is one billion and seven.
If the pair fits, add the power. Move low in. If it does not fit, the high track is too long. Move it in. Stop when the two pointers meet, and count that one too.
A fitting pair counts all between. Add a power of two. Keep it modulo a prime. Full breakdown in the lecture.