Coconut Pairs
View as PDFYou work at a coconut factory, and your job is to package coconuts into pairs for shipping. Two
coconuts can be paired together if the absolute difference between their diameters is at most .
Each coconut can be used in at most one pair.
Given the diameters of coconuts, what is the maximum number of disjoint pairs you can form?
Input
The first line contains two integers and
, the number of coconuts and the maximum allowed
diameter difference for a valid pair.
The second line contains integers, the diameters
of the coconuts.
Output
A single integer: the maximum number of disjoint coconut pairs.
Constraints
Example 1
Input
6 2
1 3 5 2 4 6
Output
3
Explanation
We can have pairs of coconuts by pairing coconuts
and
,
coconuts
and
, and coconuts
and
.
Example 2
Input
5 1
1 5 3 2 4
Output
2
Example 3
Input
7 0
1 3 1 2 2 5 4
Output
2
Comments