Minimum Difference Between Highest and Lowest of K Scores - Problem

Imagine you're a teacher selecting students for a special program based on their test scores. You have a list of student scores and need to choose exactly k students such that the performance gap between the highest and lowest scoring selected students is as small as possible.

Given a 0-indexed integer array nums where nums[i] represents the score of the i-th student, and an integer k, your task is to select any k students from the array to minimize the difference between the highest and lowest scores among your selected students.

Goal: Return the minimum possible difference between the highest and lowest scores when selecting exactly k students.

Example: If scores are [90, 30, 87, 50, 20] and k = 3, selecting students with scores [87, 90, 50] gives a difference of 90 - 50 = 40, but selecting [30, 20, 50] gives 50 - 20 = 30, which is better!

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [90,30,87,50,20], k = 3
โ€บ Output: 30
๐Ÿ’ก Note: We can select scores [30,50,20] which gives us a difference of 50-20=30. This is optimal because after sorting [20,30,50,87,90], the minimum range window of size 3 is [20,30,50].
example_2.py โ€” Small Array
$ Input: nums = [9,4,1,7], k = 2
โ€บ Output: 2
๐Ÿ’ก Note: After sorting: [1,4,7,9]. We can pick [7,9] with difference 9-7=2, or [1,4] with difference 4-1=3, or [4,7] with difference 7-4=3. The minimum is 2.
example_3.py โ€” Edge Case k=1
$ Input: nums = [87,30,12,99], k = 1
โ€บ Output: 0
๐Ÿ’ก Note: When k=1, we only select one student, so the difference between highest and lowest is always 0 (they are the same element).

Constraints

  • 1 โ‰ค k โ‰ค nums.length โ‰ค 1000
  • 0 โ‰ค nums[i] โ‰ค 105
  • k represents the exact number of students to select

Visualization

Tap to expand
๐Ÿ€ Basketball Team Selection AnalogyStep 1: Line up players by skill rating20Player A30Player B50Player C87Player D90Player EStep 2: Try different team combinations (k=3)Team Option 1203050Skill Gap: 50-20 = 30 โญTeam Option 2305087Skill Gap: 87-30 = 57Team Option 3508790Skill Gap: 90-50 = 40๐Ÿ† WINNING STRATEGYChoose Team Option 1Players with ratings [20, 30, 50]Most balanced team (gap = 30)๐Ÿ’ก Key Insight: Adjacent players in sorted lineup create the most balanced teams!
Understanding the Visualization
1
Line Up Players
Arrange all players by their skill rating from lowest to highest
2
Selection Window
Consider groups of k consecutive players in the lineup
3
Calculate Team Balance
For each group, check the skill gap between the best and worst player
4
Find Optimal Team
The group with the smallest skill gap is your most balanced team
Key Takeaway
๐ŸŽฏ Key Insight: Just like selecting consecutive players from a skill-sorted lineup minimizes team imbalance, selecting consecutive elements from a sorted array minimizes the range between max and min values!
Asked in
Amazon 15 Google 8 Meta 6 Microsoft 4
18.5K Views
Medium Frequency
~12 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen