Prosperous Pairings


Submit solution

Points: 1
Time limit: 1.0s
Python 3 1.5s
Memory limit: 256M

Author:
Problem type
Allowed languages
C++, Java, Python

Zach is studying how different pairings of people perform in group assignments in his class. He has a hypothesis: if the personalities of the two people in the pair are too similar, they will clash and cause issues. If the personalities are too dissimilar, they will not be able to relate and won't work together efficiently either.

Zach believes the ideal personality separation has a certain value k, and he wants you to find the number of so-called prosperous pairings in his class of students.

Given an array of students personality values, a pair (a,b) is considered prosperous if a and b have different indices, |a-b|=k, and both a and b exist in the array. Also, pairs (a,b) and pairs (b,a) are considered the same and are only outputted once. Find the number of prosperous pairs so that Zach can plan his assignments accordingly.

Input

The first line contains a single integer n\ (2 \leq n \leq 10^6), representing the numbers of students in Zach's class.

The second line contains a single integer k\ (0 \leq k \leq 10^4). This is the exact absolute difference that two students' personality scores must have in order to be considered a prosperous pair.

The next line contains n space-separated integers x_i\ (1 \leq x_i \leq 10^6), representing the personality score of each student.

Output

Output the number of prosperous pairs as defined above. We are only considering the unordered pairs, so (a,b) and (b,a) are not counted as distinct.

Example

Input 1
5
3
1 5 4 2 7
Output 1
3

There are 5 students in the class, and the required score difference is 3. There are 3 such valid pairs:

  • (1,4)
  • (5,2)
  • (4,7)
Input 2
7
0
1 2 3 1 2 5 1
Output 2
4

In this case, the required difference is 0, so we effectively are looking for all the pairs which are equal. I will use subscripts to denote the index of that element so to differentiate between the duplicates. They are:

  • (1_0,1_3)
  • (2_1,2_4)
  • (1_0,1_6)
  • (1_3,1_6)

Comments

There are no comments at the moment.