Daily challenge for 2026-06-25
541. 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:
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] picking 9 and 4. The difference is 5.
- [9,4,1,7] picking 9 and 1. The difference is 8.
- [9,4,1,7] picking 9 and 7. The difference is 2.
- [9,4,1,7] picking 4 and 1. The difference is 3.
- [9,4,1,7] picking 4 and 7. The difference is 3.
- [9,4,1,7] picking 1 and 7. The difference is 6.
The minimum possible difference is 2.