#C2240. Forming Maximum Teams with Height Constraint
Forming Maximum Teams with Height Constraint
Forming Maximum Teams with Height Constraint
You are given a group of N students with various heights. Your task is to form the maximum number of teams under the following conditions:
- Each team must consist of at least two students.
- Within each team, the difference between the height of the shortest and tallest student should not exceed \(K\). In other words, for any team, if \(h_{min}\) is the minimum height and \(h_{max}\) is the maximum height then \(h_{max} - h_{min} \le K\).
You need to determine the maximum number of teams that can be formed under these constraints.
Example: For input N = 6
, K = 3
, and heights [1, 2, 5, 6, 7, 10]
, the maximum number of teams is 2.
inputFormat
The first line of the input contains two integers N
and K
separated by a space.
The second line contains N
space-separated integers representing the heights of the students.
\(N\) indicates the number of students, and \(K\) represents the maximum allowed difference in heights in any valid team.
outputFormat
Output a single integer that is the maximum number of teams that can be formed following the constraints.
## sample6 3
1 2 5 6 7 10
2