#C1404. Minimum Elements Removal for Bounded Range
Minimum Elements Removal for Bounded Range
Minimum Elements Removal for Bounded Range
Given an array of N integers and an integer K, your task is to determine the minimum number of elements that need to be removed so that the difference between the maximum and minimum values of the remaining elements is at most K. In other words, you need to find a subset of the array with the maximum number of elements such that for any two elements ai and aj in the subset, the condition $$|a_j - a_i| \leq K$$ holds, and then output the difference between N and the size of this subset.
Note: The array elements may be rearranged (i.e. you can sort the array) to find the desired subset.
inputFormat
The input begins with a single integer T denoting the number of test cases. Each test case is described as follows:
- The first line contains two integers N and K.
- The second line contains N space-separated integers representing the array.
Input is provided via stdin.
outputFormat
For each test case, output a single integer representing the minimum number of elements that need to be removed. Each answer should be printed on a new line to stdout.
## sample3
5 3
4 7 2 8 5
4 10
1 2 3 20
4 0
4 4 4 4
2
1
0
</p>