Minimum Difference Between Highest and Lowest of K Scores

You are given a 0-indexed integer array `nums`, where `nums[i]` is the score of the `i`th student, and an integer `k`. Pick the scores of any `k` students so that the **difference** between the highest and the lowest of the `k` scores is **minimized**. Return the minimum possible difference.

30:00

Minimum Difference Between Highest and Lowest of K Scores
easy
Free written solution
Topics
Companies

You are given a 0-indexed integer array nums, where nums[i] is the score of the ith student, and an integer k.

Pick the scores of any k students 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
Constraints:
  • 1≤k≤nums.length≤10001 \leq k \leq \text{nums.length} \leq 1000

  • 0≤nums[i]≤1050 \leq \text{nums}[i] \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[90],"k":1}

Pick 1 of these 1 scores so that highest minus lowest is as small as possible. Only the two ends of the pick matter.

scores on a number line, k = 1

9090

scores (unsorted)

90

0

best = ?

Variables
VariableValue
k1
DepthFunction Call
Stack empty
0/4