#K73807. Count Pairs with Given Difference

    ID: 34057 Type: Default 1000ms 256MiB

Count Pairs with Given Difference

Count Pairs with Given Difference

You are given an array of n integers and an integer x. Your task is to count the number of pairs \((i, j)\) such that \(1 \leq i < j \leq n\) and \(|a_i - a_j| = x\), where \(a_i\) and \(a_j\) are elements of the array.

Input Format: The first line contains two integers \(n\) and \(x\). The second line contains \(n\) integers separated by spaces.

Example:

Input:
5 2
1 5 3 4 2

Output: 3

</p>

Explanation: In the sample case, there are three pairs with absolute difference equal to 2: (1,3), (5,3) and (3,1) when counting valid indices \(i < j\) appropriately.

Remember to use efficient methods as the array size may be large.

inputFormat

The input begins with a single line containing two integers \(n\) (the size of the array) and \(x\) (the required absolute difference). The next line contains \(n\) space-separated integers representing the array elements.

outputFormat

Output a single integer representing the number of pairs \((i, j)\) with \(1 \leq i < j \leq n\) such that \(|a_i - a_j| = x\).

## sample
5 2
1 5 3 4 2
3

</p>