Count a band by subtracting
transcript
Count the windows with a total from three to four. One pass with two limits is hard. There is a trick.
Count the windows with a total of at most four. There are ten. Count the windows at most two. There are three.
The band starts at three. So remove totals below three. That is the count at most two.
Subtract the small count from the big one. Ten minus three is seven. Seven windows lie in the band.
Each count is one pass. Move the right end along. Drop from the left while the total is too big. Then add right minus left plus one.
Neither pointer ever moves back. So both counts take two passes in all. The count can reach five billion, so use sixty-four bits.
A hundred thousand minutes have five billion windows. Trying each one is far too slow. Two passes of two pointers are enough.
Count at most high. Subtract at most low minus one. Full breakdown in the lecture.