We’re preparing your current view and syncing the latest data.
Given an integer array nums and an integer limit, determine the minimum integer k such that it is possible to add or subtract k from every element in the array to make the difference between the maximum and minimum elements less than or equal to limit. You may only add or subtract the same integer k to each element once.
An integer array nums and an integer limit.
Return the minimum integer k that satisfies the condition.
1 <= nums.length <= 10^5 0 <= nums[i], limit <= 10^9
Example 1
Input
nums = [1, 3, 6], limit = 3
Output
2
Explanation
If k = 2, we can subtract 2 from 6 making it 4, array becomes [1, 3, 4], max-min = 3 which is equal to limit.
Example 2
Input
nums = [4, 8, 10, 5], limit = 5
Output
1
Explanation
With k = 1, we can subtract 1 from 10, making it 9, array becomes [4, 8, 9, 5], max-min = 5.