#K3696. Minimum Groups Formation Under Height Constraint
Minimum Groups Formation Under Height Constraint
Minimum Groups Formation Under Height Constraint
You are given a number of test cases. In each test case, you have N students and a height threshold D. The heights of the students are provided in a list.
Your task is to form groups such that:
- Each group can contain at most 3 students.
- The difference between the height of the first student in the group (the smallest in the sorted order) and any other student in the group is at most \(D\).
You should group the students in a greedy manner starting from the shortest student. Compute the minimum number of groups required for each test case.
Note: The heights may be given in arbitrary order, so you might need to sort them first. All formulas are rendered in \(\LaTeX\) format.
inputFormat
The first line contains a single integer \(T\) denoting the number of test cases.
For each test case, the first line contains two integers \(N\) and \(D\) where \(N\) denotes the number of students and \(D\) is the maximum allowed height difference within a group. The second line contains \(N\) space-separated integers representing the heights of the students.
outputFormat
For each test case, output a single integer on a new line that represents the minimum number of groups required.
## sample2
5 2
1 3 5 6 8
4 3
4 8 5 7
3
2
</p>