Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 10000 <= nums[i] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Sliding Window
[90] 1
[9,4,1,7] 2
array-partition)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1984: Minimum Difference Between Highest and Lowest of K Scores
class Solution {
public int minimumDifference(int[] nums, int k) {
Arrays.sort(nums);
int ans = 100000;
for (int i = 0; i < nums.length - k + 1; ++i) {
ans = Math.min(ans, nums[i + k - 1] - nums[i]);
}
return ans;
}
}
// Accepted solution for LeetCode #1984: Minimum Difference Between Highest and Lowest of K Scores
func minimumDifference(nums []int, k int) int {
sort.Ints(nums)
ans := 100000
for i := 0; i < len(nums)-k+1; i++ {
ans = min(ans, nums[i+k-1]-nums[i])
}
return ans
}
# Accepted solution for LeetCode #1984: Minimum Difference Between Highest and Lowest of K Scores
class Solution:
def minimumDifference(self, nums: List[int], k: int) -> int:
nums.sort()
return min(nums[i + k - 1] - nums[i] for i in range(len(nums) - k + 1))
// Accepted solution for LeetCode #1984: Minimum Difference Between Highest and Lowest of K Scores
impl Solution {
pub fn minimum_difference(mut nums: Vec<i32>, k: i32) -> i32 {
nums.sort();
let k = k as usize;
let mut res = i32::MAX;
for i in 0..=nums.len() - k {
res = res.min(nums[i + k - 1] - nums[i]);
}
res
}
}
// Accepted solution for LeetCode #1984: Minimum Difference Between Highest and Lowest of K Scores
function minimumDifference(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
const n = nums.length;
let ans = nums[n - 1] - nums[0];
for (let i = 0; i + k - 1 < n; i++) {
ans = Math.min(nums[i + k - 1] - nums[i], ans);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.