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 i^th 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
Inputnums = [90], k = 1
Output0
There is one way to pick one score, so the highest and lowest score are both 90 and the difference is 0.
Example 2
Inputnums = [9,4,1,7], k = 2
Output2
Among all ways to pick two scores, choosing 9 and 7 gives the minimum possible difference of 2.

Constraints

  • 1 <= k <= nums.length <= 1000
  • 0 <= nums[i] <= 10^5

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate