Imagine you have an array of numbers and you're given a magical power to transform each element! For every number in the array, you must choose to either add k or subtract k from it - no exceptions, no staying the same.
Your goal is to minimize the range (difference between maximum and minimum values) of the transformed array. This is like trying to bring all the numbers as close together as possible after applying your transformations.
Example: If you have [1, 3, 6] and k = 3, you could transform it to [4, 0, 9] (adding 3, subtracting 3, adding 3) giving a range of 9, or [4, 6, 3] (adding 3, adding 3, subtracting 3) giving a range of 3. The second choice is better!
Input: An integer array nums and an integer k
Output: The minimum possible range after transforming all elements
Input & Output
Constraints
- 1 โค nums.length โค 104
- 0 โค nums[i] โค 104
- 0 โค k โค 104