Counting Pairs
Problem statement
You are given an integer array numbers and a nonnegative integer k. Count the number of distinct value pairs (a, b) for which both values occur in numbers and a + k = b.
Pairs are distinguished by their values, not by the indices or number of occurrences. Duplicate array elements therefore do not create duplicate pairs. When k = 0, each distinct value x contributes the pair (x, x); one occurrence of x is sufficient.
Function
countPairs(numbers: int[], k: int) → intExamples
Example 1
numbers = [1, 1, 1, 2]k = 1return = 1The only distinct value pair with difference 1 is (1, 2). The three occurrences of 1 do not create additional value pairs, so the answer is 1.
Example 2
numbers = [1, 2]k = 0return = 2Because k = 0, each distinct value forms one pair with itself. The values 1 and 2 contribute (1, 1) and (2, 2), so the answer is 2.
Constraints
2 <= numbers.length <= 2000000 <= numbers[i] <= 10000000000 <= k <= 1000000000