Description
You are given an integer array nums and an integer k. For each index i, you must add k or -k to nums[i]. Return the minimum difference between the maximum and minimum values after modifications.
Examples
Input:
nums = [1], k = 0Output:
0Explanation:
Only one element, diff is 0.
Input:
nums = [1, 3, 6], k = 3Output:
3Explanation:
It is possible to modify each element by adding or subtracting 3. The optimal strategy is to add 3 to smaller elements and subtract 3 from larger elements to minimize the range. This gives: [1+3, 3+3, 6-3] = [4, 6, 3]. The difference between max(6) and min(3) is 3.
Input:
nums = [2, 7, 2], k = 1Output:
3Explanation:
It is possible to add or subtract 1 from each element. The possible modifications are: [2±1, 7±1, 2±1]. To minimize the range, choosing [2+1, 7-1, 2+1] = [3, 6, 3]. The difference between max(6) and min(3) is 3.
Constraints
- •
1 ≤ nums.length ≤ 10^4 - •
0 ≤ nums[i] ≤ 10^4 - •
0 ≤ k ≤ 10^4