60. Minimum Difference Between Highest and Lowest of K Scores

EasyArrayArray

You are given an integer array nums (test scores) and an integer k. Pick any k scores from the array. Return the minimum possible difference between the highest and lowest of the picked scores.

Input: An integer array nums and an integer k.

Output: Integer — minimum (max - min) among any k chosen scores.

Examples

Example 1
Input: [90],1
Output: 0
Explanation: Pick [90]. max-min=0.
Example 2
Input: [9,4,1,7],2
Output: 2
Explanation: Sort: [1,4,7,9]. Best window of 2: [7,9]→2 or [1,4-1=3? No [4,7]→3,[7,9]→2. Min=2.
Example 3
Input: [87038,55569],2
Output: 31469
Explanation: Only one window of 2. 87038-55569=31469.

Constraints

Asked by

AmazonGoogleMicrosoftBloombergMeta
Solve this problem in the editor →