1984. Minimum Difference Between Highest and Lowest of K Scores LeetCode Solution
In this guide, you will get 1984. Minimum Difference Between Highest and Lowest of K Scores LeetCode Solution with the best time and space complexity. The solution to Minimum Difference Between Highest and Lowest of K Scores problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Difference Between Highest and Lowest of K Scores solution in C++
- Minimum Difference Between Highest and Lowest of K Scores solution in Java
- Minimum Difference Between Highest and Lowest of K Scores solution in Python
- Additional Resources

Problem Statement of 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
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(\texttt{sort})
1984. Minimum Difference Between Highest and Lowest of K Scores LeetCode Solution in C++
class Solution {
public:
int minimumDifference(vector<int>& nums, int k) {
ranges::sort(nums);
int ans = nums[k - 1] - nums[0];
for (int i = k; i < nums.size(); ++i)
ans = min(ans, nums[i] - nums[i - k + 1]);
return ans;
}
};
/* code provided by PROGIEZ */
1984. Minimum Difference Between Highest and Lowest of K Scores LeetCode Solution in Java
class Solution {
public int minimumDifference(int[] nums, int k) {
Arrays.sort(nums);
int ans = nums[k - 1] - nums[0];
for (int i = k; i < nums.length; ++i)
ans = Math.min(ans, nums[i] - nums[i - k + 1]);
return ans;
}
}
// code provided by PROGIEZ
1984. Minimum Difference Between Highest and Lowest of K Scores LeetCode Solution in Python
class Solution:
def minimumDifference(self, nums: list[int], k: int) -> int:
nums.sort()
ans = nums[k - 1] - nums[0]
for i in range(k, len(nums)):
ans = min(ans, nums[i] - nums[i - k + 1])
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.