Loading...
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.
Input: nums = [90], k = 1
Output: 0
Explanation: Only one value can be chosen, and its spread against itself is 90 - 90 = 0.
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.
nums.length ≤1000nums[i] ≤105Click "Run" to test with sample cases or "Submit" to run all tests.