Ctrl K

Fewest Tight Groups

Medium
You are given an integer array values and an integer spread. Distribute all the numbers into groups: each number goes into exactly one group, and order within a group does not matter. A group is tight if its largest number minus its smallest number is at most spread. Return the smallest number of groups such that every group is tight.
Example 1
values=[9, 1, 4, 10, 3], spread=3 → 2

Groups {1, 3, 4} and {9, 10}: in each, the largest minus the smallest is at most 3. One group is not enough, since 10 − 1 = 9.

Example 2
values=[7, 7, 8], spread=0 → 2

With a spread of 0 a group may only hold equal numbers: {7, 7} and {8}.

Example 3
values=[5], spread=2 → 1
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁵
  • 0 ≤ spread ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
values=[9, 1, 4, 10, 3], spread=3
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 113 tests on the server, hidden ones included.