Skip to main content
Back to problems
#1984
Easy Algorithms

Minimum difference between highest and lowest of k scores

Array Sliding Window Sorting
66.2% acceptance
Feb 25, 2026
1450
374
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.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn minimum_difference(nums: Vec<i32>, k: i32) -> i32 {
    let mut nums = nums;
    nums.sort();
    let k = k as usize;
    let mut min_diff = i32::MAX;
    for i in 0..=nums.len() - k {
      min_diff = min_diff.min(nums[i + k - 1] - nums[i]);
    }
    min_diff
  }
}