Playlist Pair Lengths
Medium · two pointers
A radio host has a playlist of songs. The list lengths holds the length of each song in seconds. The host wants to play two different songs back to back as a short block, and the block must last between lo and hi seconds, both included.
Count the pairs of songs, taken from two different positions i < j in the list, whose two lengths add up to a total from lo to hi. Two songs with the same length are still different songs, and a pair is counted once no matter which of the two plays first.
Return the number of such pairs.
Examples
Example 1
Input: lengths = [3, 5, 2, 8], lo = 7, hi = 10
Output: 3
The pairs 3 + 5 = 8, 5 + 2 = 7 and 2 + 8 = 10 fit. The others total 5, 11 and 13.
Example 2
Input: lengths = [4, 4, 4], lo = 8, hi = 8
Output: 3
All three pairs of songs total exactly 8, even though the lengths are equal.
Example 3
Input: lengths = [10], lo = 1, hi = 100
Output: 0
One song cannot make a pair.
Constraints
- 1 <= len(lengths) <= 3000
- 1 <= lengths[i] <= 10^9
- 1 <= lo <= hi <= 2 * 10^9