{x}
blog image

Minimum Difference Between Highest and Lowest of K Scores

You are given a 0-indexed integer array nums, where nums[i] represents the score of the ith student. You are also given an integer k.

Pick the scores of any k students from the array so that the difference between the highest and the lowest of the k scores is minimized.

Return the minimum possible difference.

 

Example 1:

Input: nums = [90], k = 1
Output: 0
Explanation: There is one way to pick score(s) of one student:
- [90]. The difference between the highest and lowest score is 90 - 90 = 0.
The minimum possible difference is 0.

Example 2:

Input: nums = [9,4,1,7], k = 2
Output: 2
Explanation: There are six ways to pick score(s) of two students:
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 4 = 5.
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 1 = 8.
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 7 = 2.
- [9,4,1,7]. The difference between the highest and lowest score is 4 - 1 = 3.
- [9,4,1,7]. The difference between the highest and lowest score is 7 - 4 = 3.
- [9,4,1,7]. The difference between the highest and lowest score is 7 - 1 = 6.
The minimum possible difference is 2.

 

Constraints:

  • 1 <= k <= nums.length <= 1000
  • 0 <= nums[i] <= 105

Solution Explanation: Minimum Difference Between Highest and Lowest of K Scores

The problem asks to find the minimum difference between the highest and lowest scores among any k students selected from a given array of scores.

Approach:

The most efficient approach involves these steps:

  1. Sorting: Sort the nums array in ascending order. This allows us to easily find the minimum and maximum scores within a window of size k.

  2. Sliding Window: Use a sliding window of size k to traverse the sorted array. For each window:

    • The minimum score is the first element of the window (nums[i]).
    • The maximum score is the last element of the window (nums[i + k - 1]).
    • Calculate the difference between the maximum and minimum scores (nums[i + k - 1] - nums[i]).
    • Update the minimum difference found so far.
  3. Return Minimum Difference: After iterating through all possible windows, return the minimum difference found.

Time Complexity Analysis:

  • Sorting the array takes O(n log n) time, where n is the length of the nums array.
  • The sliding window iteration takes O(n) time.
  • Therefore, the overall time complexity is dominated by the sorting step, resulting in O(n log n).

Space Complexity Analysis:

  • In-place sorting algorithms can be used, meaning we don't need extra space proportional to the input size.
  • The space used for the ans variable and the window is constant, O(1).
  • Thus, the space complexity is O(log n) (due to the recursive calls in some sorting algorithms or O(1) if using in-place algorithms).

Code Implementation (Python):

class Solution:
    def minimumDifference(self, nums: List[int], k: int) -> int:
        nums.sort()  # Sort the array in ascending order
        min_diff = float('inf')  # Initialize min_diff to a large value
 
        for i in range(len(nums) - k + 1):
            diff = nums[i + k - 1] - nums[i]  # Calculate the difference
            min_diff = min(min_diff, diff)  # Update min_diff if necessary
 
        return min_diff

Code Implementation (Java):

import java.util.Arrays;
 
class Solution {
    public int minimumDifference(int[] nums, int k) {
        Arrays.sort(nums); // Sort the array
        int minDiff = Integer.MAX_VALUE; // Initialize minDiff to a large value
 
        for (int i = 0; i <= nums.length - k; i++) {
            int diff = nums[i + k - 1] - nums[i]; // Calculate the difference
            minDiff = Math.min(minDiff, diff); // Update minDiff
        }
 
        return minDiff;
    }
}

The other language implementations follow a very similar structure, adapting the sorting and min/max functions to the specific language. The core algorithm remains the same.