You are given an integer array nums and an integer k.
In one operation, you can choose any index i where 0 <= i < nums.length and change nums[i] to nums[i] + x where x is an integer from the range [-k, k]. You can apply this operation at most once for each index i.
The score of nums is the difference between the maximum and minimum elements in nums.
Return the minimum score of nums after applying the mentioned operation at most once for each index in it.
Example 1:
Input: nums = [1], k = 0 Output: 0 Explanation: The score is max(nums) - min(nums) = 1 - 1 = 0.
Example 2:
Input: nums = [0,10], k = 2 Output: 6 Explanation: Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.
Example 3:
Input: nums = [1,3,6], k = 3 Output: 0 Explanation: Change nums to be [4, 4, 4]. The score is max(nums) - min(nums) = 4 - 4 = 0.
Constraints:
1 <= nums.length <= 1040 <= nums[i] <= 1040 <= k <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to find the smallest range involves checking every possible adjustment we can make to each number. We'll try adding or subtracting the maximum allowed adjustment from each number, then calculate the range between the biggest and smallest numbers after these adjustments.
Here's how the algorithm would work step-by-step:
def smallest_range_brute_force(numbers, allowable_amount):
smallest_range = float('inf')
# Iterate through each number in the input list
for index in range(len(numbers)):
# Try adding the allowable amount to the number
adjusted_numbers_addition = numbers[:]
adjusted_numbers_addition[index] += allowable_amount
maximum_value_addition = max(adjusted_numbers_addition)
minimum_value_addition = min(adjusted_numbers_addition)
current_range_addition = maximum_value_addition - minimum_value_addition
smallest_range = min(smallest_range, current_range_addition)
# Try subtracting the allowable amount from the number
adjusted_numbers_subtraction = numbers[:]
adjusted_numbers_subtraction[index] -= allowable_amount
maximum_value_subtraction = max(adjusted_numbers_subtraction)
minimum_value_subtraction = min(adjusted_numbers_subtraction)
current_range_subtraction = maximum_value_subtraction - minimum_value_subtraction
smallest_range = min(smallest_range, current_range_subtraction)
return smallest_rangeThe goal is to minimize the difference between the largest and smallest numbers in a group after making adjustments to each number. Instead of changing every number, we can focus on the largest and smallest values to bring them closer together. By moving the largest number down and the smallest number up as much as possible, we can quickly find the smallest possible range.
Here's how the algorithm would work step-by-step:
def smallest_range(number_list, adjustment_value):
largest_number = max(number_list)
smallest_number = min(number_list)
# If the range can be reduced to zero.
if smallest_number + adjustment_value >= largest_number - adjustment_value:
return 0
# Calculate smallest possible range.
smallest_possible_range = (largest_number - adjustment_value) - \
(smallest_number + adjustment_value)
# Return the result.
return smallest_possible_range| Case | How to Handle |
|---|---|
| Empty or null input array | Return 0 since there are no numbers to modify and thus the difference between max and min is zero. |
| Array with only one element | Return 0 since there's only one number and the difference between the (same) max and min is zero. |
| k is zero | The score is the original difference between max and min of the input array. |
| All elements in the array are identical | The minimum score is 0, achievable by adding 0 to each element. |
| k is very large (larger than half the difference between max and min) | The minimum score will be 0, because all numbers can be made equal by adding or subtracting k. |
| Array with large positive and negative numbers | The min and max values could potentially lead to integer overflow when computing the difference, so use long to handle it. |
| Input array is already at its minimum possible score of 0 | The algorithm should still correctly return 0 since the difference between max and min is 0 already. |
| Negative k value | The problem statement defined k as an integer and [-k, k] should be the proper range for the addition, and we should take the absolute value to keep its logic. |