You are given an integer array nums and an integer k.
Choose any k values from nums by position, so a value that appears more than once may be chosen more than once. The largest chosen value minus the smallest chosen value is the spread of that choice.
Return the smallest spread achievable over any choice of k values.
When k is 1, the chosen value is both the largest and the smallest, so the spread is 0.
Examples
Example 1
Input: nums = [90], k = 1
Output: 0
Explanation: Only one value can be chosen, and its spread against itself is 90 - 90 = 0.
Example 2
Input: nums = [9,4,1,7], k = 2
Output: 2
Explanation: The six possible pairs have spreads 5, 8, 2, 3, 3, and 6. The smallest is 2, from the pair 9 and 7.
Constraints
1≤k≤nums.length≤1000
0≤nums[i]≤105
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up