#C4496. Count Pairs with Given Difference

    ID: 48040 Type: Default 1000ms 256MiB

Count Pairs with Given Difference

Count Pairs with Given Difference

You are given an array A of n integers and an integer k. Your task is to count the number of pairs (i, j) (with i < j) such that the absolute difference between A[i] and A[j] is exactly k. In mathematical terms, you need to find the number of pairs satisfying \( |A_i - A_j| = k \).

Example: For A = [1, 5, 3, 4, 2] and k = 2, the valid pairs are (1, 3), (5, 3), (3, 1) (depending on positions) resulting in an answer of 3.

You are required to solve the problem by reading input from stdin and writing the output to stdout. Each test case should be processed independently.

inputFormat

The first line of the input contains an integer T denoting the number of test cases.

Each test case consists of two lines:

  • The first line contains two integers: n (the number of elements in the array) and k (the required difference).
  • The second line contains n space-separated integers representing the array A.

outputFormat

For each test case, output a single line containing the number of pairs (i, j) such that i < j and \( |A_i - A_j| = k \).

## sample
1
5 2
1 5 3 4 2
3

</p>