#C6819. Count Pairs with a Given Difference

    ID: 50621 Type: Default 1000ms 256MiB

Count Pairs with a Given Difference

Count Pairs with a Given Difference

You are given a list of integers and an integer (K). Your task is to count the number of unique pairs ((a, b)) in the list such that the absolute difference between (a) and (b) is exactly (K).

More formally, given a list (A = [a_1, a_2, \dots, a_n]) and an integer (K), you need to count the number of pairs ((a, b)) with (a, b \in A) and (a \neq b) satisfying

[ |a - b| = K. ]

Note that each pair is counted only once, i.e. the pair ((a, b)) is considered the same as ((b, a)). An equivalent formulation is to count the number of elements (x) in the set (S) corresponding to (A) for which (x + K) is also in (S):

[ \text{answer} = \sum_{x \in S} \mathbf{1}_{{x+K \in S}}. ]

The input will be given from standard input and the answer should be printed to standard output.

inputFormat

The first line contains two integers (n) and (K), where (n) is the number of elements in the list. The second line contains (n) space-separated integers representing the list (A).

outputFormat

Output a single integer: the number of unique pairs with an absolute difference of (K).## sample

5 3
4 1 5 2 6
2