Smallest Range I
The problem essentially boils down to minimizing the difference between the maximum and minimum values in the list. To accomplish this, we can increase the smaller numbers and decrease the larger numbers. In the context of the problem, this translates to adding k
to the minimum number and subtracting k
from the maximum number.
Here’s the Python code:


This function works by:
Identifying the maximum and minimum values in
nums
using themax
andmin
functions.Subtracting the minimum value from the maximum value. This gives us the current range of
nums
.From this range, subtracting
2*k
. We subtract2*k
because we can increase the minimum value byk
and decrease the maximum value byk
, effectively reducing the range by2*k
.If the range after subtracting
2*k
is negative, it means that we have overcompensated for the range and all elements are now equal, so the function returns0
. This is accomplished by taking the maximum of0
and the calculated range.
This function returns the minimum possible score (which is the range of nums
after applying the operation). It assumes that you can choose any x
in the range [k, k]
when modifying the elements of nums
, and that the operation can be applied at most once for each element.
Here’s a version with intermediate steps:


This code does the same thing as the previous one, but each step is broken down into separate lines. This makes it easier to see what each step is doing and how the final result is calculated.
Q&A
i cant even understand the question can somenone explain me the question properly
The question gives you an array of integers (nums) and a single integer (k). The score of an array is defined as the difference between the maximum and minimum elements in the array.
You have the ability to perform one operation: for any element in the array, you can add or subtract a value ‘x’ to it, where ‘x’ can be any integer value from k to k (inclusive).
The twist is that you can only apply this operation at most once for each element in the array. After performing these operations, the objective is to minimize the score of the array.
The question is asking for the minimum possible score of the array after performing these operations.
Here’s an example for better understanding:
Let’s say, nums = [1,3,6] and k = 3. You can add 3 to the 1 to get 4, subtract 3 from the 6 to get 3, and add 1 to the 3 to get 4. Now, your new array is [4,4,4]. The difference between the maximum and minimum elements is 0, which is the smallest possible score. So, the answer for this input would be 0.
If min(A) + K < max(A)  K, then return max(A)  min(A)  2 * K If min(A) + K >= max(A)  K, then return 0
Firstly, the score of the array is the difference between the maximum and the minimum elements in the array. The aim of the problem is to minimize this score. How can we do this? By bringing the maximum and minimum elements closer to each other using the operation mentioned in the problem.
Now, we have a value k
which can be added or subtracted to each element in the array. Ideally, we would want to increase the minimum value by k
and decrease the maximum value by k
to reduce the score.
So, after performing these operations, the new minimum becomes min(A) + k
and the new maximum becomes max(A)  k
.
Now, there are two cases to consider:
If
min(A) + k
is less thanmax(A)  k
, this means that even after increasing the minimum byk
and decreasing the maximum byk
, the minimum is still less than the maximum. In this case, there is a remaining gap between the new minimum and maximum that we cannot close further, because we have already used our one operation per element. The remaining gap would bemax(A)  min(A)  2*k
.If
min(A) + k
is greater than or equal tomax(A)  k
, this means that we have successfully closed the gap between the minimum and maximum to 0 or less (the minimum is greater than the maximum). In this case, the score of the array would be 0 because there’s no difference between the maximum and minimum values.
Therefore, the answer would be max(A)  min(A)  2 * k
if min(A) + k
is less than max(A)  k
, else it would be 0.
How did you got to the formula you wrote from problem statement?
The problem asks us to minimize the difference between the largest and smallest elements of the array by at most adding or subtracting k
from each element. This translates into getting the elements as close to each other as possible.
Here’s the step by step logic behind the formula:
Identify what we’re trying to minimize: From the problem, we know that we’re trying to minimize the difference between the largest and smallest elements. In mathematical terms, this is
max(A)  min(A)
.Understand the effect of our actions: The problem states that we can add or subtract
k
from each element. This means we can increase the smallest element byk
(which would decrease the difference) and decrease the largest element byk
(which would also decrease the difference). Mathematically, this translates to changingmin(A)
tomin(A) + k
andmax(A)
tomax(A)  k
.Calculate the new difference: With the changes from step 2, the new difference would be
(max(A)  k)  (min(A) + k) = max(A)  min(A)  2*k
.Consider the constraints: We’re limited by
k
. We cannot subtract more thank
from the maximum or add more thank
to the minimum. Therefore, ifmin(A) + k < max(A)  k
, then we can’t completely close the gap between the maximum and minimum. In this case, the remaining difference would bemax(A)  min(A)  2*k
. Ifmin(A) + k >= max(A)  k
, we can close the gap completely, and the difference would be 0.
So, that’s the logic behind the formula. It comes from understanding what we’re trying to achieve (minimizing the difference between the maximum and minimum) and how our actions (adding or subtracting k
) affect that goal.
Key Takeaway
This problem is a good example that illustrates the skill needed to come up with a formula from problem statement.