Largest group of students whose skill levels differ by at most 5

Quick Overview

Given a list of student skill levels, find the largest group you can pick so that the highest and lowest skill differ by at most 5. It tests whether you see that sorting turns the choice into a contiguous window, and then a two-pointer scan over the sorted values.

Largest group of students whose skill levels differ by at most 5

Company: Swiggy

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text 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. ```text Input: skills = [1, 1, 1, 1, 1] Output: 5 ``` All students have the same skill level, so all 5 can be selected. ```text 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.

Overview: Given a list of student skill levels, find the largest group you can pick so that the highest and lowest skill differ by at most 5. It tests whether you see that sorting turns the choice into a contiguous window, and then a two-pointer scan over the sorted values.

Swiggy logo
Swiggy
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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.