#C3966. Count Pairs with a Given Difference

    ID: 47451 Type: Default 1000ms 256MiB

Count Pairs with a Given Difference

Count Pairs with a Given Difference

You are given an array of integers and an integer ( k ). Your task is to count the number of unordered pairs ( (i, j) ) (with ( i \neq j )) such that ( |a_i - a_j| = k ).

Note that each pair is counted only once. For example, if ( k = 2 ) and the array is [1, 5, 3, 4, 2], the valid pairs are ( (1,3) ), ( (3,5) ), and ( (2,4) ), yielding an answer of 3.

It is guaranteed that ( k ) is a non-negative integer. The input is taken from standard input and the result should be printed to standard output.

inputFormat

The first line contains two integers ( n ) and ( k ) separated by a space, where ( n ) is the number of elements in the array and ( k ) is the given difference.

The second line contains ( n ) space-separated integers representing the elements of the array.

outputFormat

Output a single integer that denotes the number of pairs ( (i, j) ) such that ( |a_i - a_j| = k ) and ( i \neq j ).## sample

5 2
1 5 3 4 2
3