Pairs with a difference
Write pairs_with_difference(nums, k) that returns how many distinct
pairs of values in nums differ by exactly k, where k is positive.
A pair is a set of two values, so (1, 3) and (3, 1) are the same
pair, and repeated values count once.
pairs_with_difference([1, 5, 3, 4, 2], 2) -> 3 (1,3) (3,5) (2,4)
pairs_with_difference([1, 1, 3], 2) -> 1
pairs_with_difference([1, 2, 3], 5) -> 0
The point is to practise looking up the complement. One check has a hundred thousand values; the pairwise loop will not finish in time.
def pairs_with_difference(nums, k):
...