Given an array of integers skills representing student skill levels, return the maximum number of students you can select such that the difference between the maximum and minimum skill value in the selected group is at most 5.
You may select any subset of students; the order of selection does not matter.
Function Signature
def max_balanced_team_size(skills: list[int]) -> int:
Rules
-
A group is valid when its largest skill minus its smallest skill is at most 5. A difference of exactly 5 is allowed.
-
Each array entry is a separate student. Students with the same skill level are all counted.
-
The selected students do not have to be adjacent in the input array.
-
Return only the size of the largest valid group.
Constraints
-
1 <= len(skills) <= 10^5
-
0 <= skills[i] <= 10^9
Examples
Input: skills = [10, 4, 7, 2, 6, 8, 3, 12]
Output: 5
Sorted, the skills are [2, 3, 4, 6, 7, 8, 10, 12]. The groups [2, 3, 4, 6, 7] and [3, 4, 6, 7, 8] each have size 5 and a spread of exactly 5, and no 6 students fit within a spread of 5.
Input: skills = [1, 1, 1, 1, 1]
Output: 5
All students have the same skill level, so all 5 can be selected.
Input: skills = [5, 0, 11]
Output: 2
The students with skills 0 and 5 differ by exactly 5, which is allowed. Adding 11 would make the spread 11.