A choir director has assigned each of n choir members an integer pitch level. Given a target interval k, count the number of pairs of choir members (i, j) with i < j such that the absolute difference between their pitch levels equals exactly k.
Print a single integer: the number of member pairs whose pitch levels differ by exactly k.
Example 1
Input
5 2 1 3 5 7 9
Expected
4
Explanation
Pitches 1,3,5,7,9 form a chain where each consecutive pair differs by exactly 2: (1,3), (3,5), (5,7), (7,9) -- 4 pairs. Non-consecutive pairs like (1,5) or (3,7) differ by 4, not 2, so they are not counted. Total = 4.
Example 2
Input
4 3 4 4 7 1
Expected
4
Explanation
Pitches at positions 0..3 are [4,4,7,1]. Pairs differing by exactly 3: position 0 & 2 (|4-7|=3), position 1 & 2 (|4-7|=3), position 0 & 3 (|4-1|=3), position 1 & 3 (|4-1|=3) -- 4 qualifying pairs. Position 2 & 3 differ by 6 and position 0 & 1 differ by 0, so neither counts. Total = 4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →