944. Smallest Range I
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum possible difference between the maximum and minimum elements of an array nums after applying a specific operation. The operation allows us to change any element nums[i] to nums[i] + x, where x is an integer in the range [-k, k]. This can be done at most once for each element.
Solution
Smallest Range I in Python
class Solution:
def smallestRangeI(self, nums: List[int], k: int) -> int:
# Find the minimum and maximum elements in the array.
min_val = min(nums)
max_val = max(nums)
# The initial score (difference) before any operations.
diff = max_val - min_val
# To minimize the score, we want to increase the minimum value
# and decrease the maximum value.
# The minimum can be increased by at most k.
# The maximum can be decreased by at most k.
# This means the total possible reduction in the difference is 2*k.
potential_ans = diff - 2 * k
# The score cannot be negative. If `potential_ans` is less than or equal to 0,
# it means we can make the new max and min equal, so the score is 0.
return max(0, potential_ans)
Smallest Range I in Java
class Solution {
public int smallestRangeI(int[] nums, int k) {
// Find the minimum and maximum elements in the array.
// A single pass is efficient for this.
int min_val = nums[0];
int max_val = nums[0];
for (int num : nums) {
min_val = Math.min(min_val, num);
max_val = Math.max(max_val, num);
}
// The initial score (difference) before any operations.
int diff = max_val - min_val;
// To minimize the score, we want to increase the minimum value
// and decrease the maximum value.
// The minimum can be increased by at most k.
// The maximum can be decreased by at most k.
// This means the total possible reduction in the difference is 2*k.
int potential_ans = diff - 2 * k;
// The score cannot be negative. If `potential_ans` is less than or equal to 0,
// it means we can make the new max and min equal, so the score is 0.
return Math.max(0, potential_ans);
}
}
Smallest Range I in C++
class Solution {
public:
int smallestRangeI(vector<int>& nums, int k) {
// Find the minimum and maximum elements in the array.
// Using *min_element and *max_element is concise.
// A single loop could also be used for a slight performance gain.
int min_val = *min_element(nums.begin(), nums.end());
int max_val = *max_element(nums.begin(), nums.end());
// The initial score (difference) before any operations.
int diff = max_val - min_val;
// To minimize the score, we want to increase the minimum value
// and decrease the maximum value.
// The minimum can be increased by at most k.
// The maximum can be decreased by at most k.
// This means the total possible reduction in the difference is 2*k.
int potential_ans = diff - 2 * k;
// The score cannot be negative. If `potential_ans` is less than or equal to 0,
// it means we can make the new max and min equal, so the score is 0.
return max(0, potential_ans);
}
};
Smallest Range I in TypeScript
function smallestRangeI(nums: number[], k: number): number {
// Find the minimum and maximum elements in the array.
// Using Math.min(...nums) and Math.max(...nums) can cause a stack overflow for large arrays.
// A manual loop is safer and more performant.
let min_val = nums[0];
let max_val = nums[0];
for (const num of nums) {
min_val = Math.min(min_val, num);
max_val = Math.max(max_val, num);
}
// The initial score (difference) before any operations.
const diff = max_val - min_val;
// To minimize the score, we want to increase the minimum value
// and decrease the maximum value.
// The minimum can be increased by at most k.
// The maximum can be decreased by at most k.
// This means the total possible reduction in the difference is 2*k.
const potential_ans = diff - 2 * k;
// The score cannot be negative. If `potential_ans` is less than or equal to 0,
// it means we can make the new max and min equal, so the score is 0.
return Math.max(0, potential_ans);
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.