#C12728. Count Pairs with a Given Difference
Count Pairs with a Given Difference
Count Pairs with a Given Difference
In this problem, you are given an array of integers (\textbf{nums}) and a non-negative integer (k). Your task is to count the number of distinct pairs ((i, j)) such that (|nums_i - nums_j| = k). A pair is considered distinct if the pair of values is unique, i.e., the pairs ((i, j)) and ((j, i)) are treated as the same.
Note that when (k = 0), a pair is counted only once if there are duplicate elements.
For example, for (nums = [1, 5, 3, 4, 2]) and (k = 2), there are three distinct pairs: ((1,3)), ((3,5)), and ((2,4)).
inputFormat
The first line of input contains two integers (n) and (k) where (n) is the number of elements in the array and (k) is the required difference. The second line contains (n) space-separated integers representing the elements of the array. Note that (n) can be zero, in which case the array is empty.
outputFormat
Output a single integer representing the number of distinct pairs whose absolute difference is equal to (k).## sample
5 10
1 2 3 4 5
0