A gap of zero counts twice
transcript
Count the pairs of plays that differ by a gap. It passes every sample. Then it fails on a gap of zero. Samples rarely contain a gap of zero.
Each play has two partners. One is the value minus the gap. The other is the value plus the gap. Both look-ups find earlier plays only.
With a gap of zero, both partners are one value. Counting both gives two pairs. There is only one pair of three and three. The fix is one small check.
Count the second partner only when the gap is not zero. Record each play after you look up its partners. Then a play never pairs with itself. Both rules take a single line each.
The count can reach almost five billion. That is above the thirty-two-bit limit. So use a sixty-four-bit count. Always keep pair counts in a wide type.
Use two equal plays. The answer must be one. Full breakdown in the lecture.