Prosperous Pairings
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 , 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 is considered prosperous if
and
have different indices,
, and both
and
exist in the array. Also, pairs
and pairs
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 , representing the numbers of students in Zach's class.
The second line contains a single integer . 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 space-separated integers
, 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 and
are not counted as distinct.
Example
Input 1
5
3
1 5 4 2 7
Output 1
3
There are students in the class, and the required score difference is
. There are
such valid pairs:
Input 2
7
0
1 2 3 1 2 5 1
Output 2
4
In this case, the required difference is , 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:
Comments