Skip to content
← All posts

Minimum Difference Between Highest and Lowest of K Scores

3 min read
leetcodeproblemeasyarrayssortingsliding-window

LeetCode 1984, Minimum Difference Between Highest and Lowest of K Scores, gives you an array of scores and asks you to pick k scores such that the difference between the highest and lowest chosen score is minimized.

10417293diff = 9 - 7 = 2 (min)
Sorted array [1, 4, 7, 9] with k=2. The window [7, 9] gives the smallest difference of 2.

The approach: sort and slide

The key insight: after sorting, the k scores with the smallest range must be consecutive. If they were not consecutive, swapping a non-adjacent element for the one between would either maintain or reduce the range. So you sort the array, then slide a window of size k across it, computing nums[i + k - 1] - nums[i] at each position. The minimum of these differences is the answer.

The code

def minimumDifference(nums, k):
    nums.sort()
    return min(nums[i + k - 1] - nums[i] for i in range(len(nums) - k + 1))

Two lines of real logic. Sort the array, then check every consecutive group of k elements and return the smallest gap between the first and last.

Visual walkthrough

Let's trace through nums = [9, 4, 1, 7] with k = 2. After sorting, we get [1, 4, 7, 9]. Now we slide a window of size 2 across the sorted array and compute the difference at each position.

Step 1: Window at indices [0, 1]. Values: [1, 4].

10417293diff = 34 - 1 = 3

Difference = 4 - 1 = 3. Current minimum = 3.

Step 2: Window at indices [1, 2]. Values: [4, 7].

10417293diff = 37 - 4 = 3

Difference = 7 - 4 = 3. Still tied at minimum = 3.

Step 3: Window at indices [2, 3]. Values: [7, 9].

10417293diff = 29 - 7 = 2

Difference = 9 - 7 = 2. New minimum! Answer = 2.

The window [7, 9] gives the smallest difference of 2, so the answer is 2.

Complexity analysis

MetricValue
TimeO(n log n) for the sort, O(n) for the scan
SpaceO(1) extra (or O(n) depending on sort implementation)

The sort dominates. The sliding window scan is linear, visiting each valid window position exactly once.

Building blocks

This problem combines two reusable ideas:

1. Sorting to make optimal subsets contiguous

When you need to minimize the range of a chosen subset, sorting guarantees that any optimal group forms a contiguous slice. This same principle appears in problems like assigning cookies, splitting arrays, and interval scheduling. Once the array is sorted, you reduce a combinatorial search to a linear scan.

2. Fixed-size sliding window

After sorting, you slide a window of exactly k elements across the array. At each position, you compute a value (here, the difference between the last and first element in the window) and track the best result. This is the simplest form of a sliding window: fixed size, no shrinking or expanding needed.

for i in range(len(nums) - k + 1):
    diff = nums[i + k - 1] - nums[i]

Edge cases

  • k equals 1: the difference is always 0, because you pick a single element and its "range" is zero
  • k equals n: there is only one window covering the entire array, so the difference is nums[-1] - nums[0] after sorting
  • All elements are the same: every window has difference 0
  • Array of length 1: k must be 1, and the difference is 0

From understanding to recall

This problem is easy to understand once you see the sort-and-slide approach. But under interview pressure, the details matter. Do you remember to compute nums[i + k - 1] - nums[i] and not nums[i + k] - nums[i]? Do you set the loop bound to len(nums) - k + 1? These off-by-one details are exactly what trips people up when they have not practiced recently.

Spaced repetition helps you lock in the pattern. You write the sort, the loop, and the difference formula from scratch at increasing intervals. After a few cycles, the code comes out automatically, and you can focus your interview energy on harder problems.

Related posts

  • Contains Duplicate - Another easy array problem that uses sorting as one of its solution paths
  • Sliding Window Maximum - A harder sliding window problem where you track the max inside each window using a monotonic deque
  • The Sliding Window Pattern - The general sliding window template that covers both fixed-size and variable-size windows