KatInterview

946. Smallest Range II

MediumSolutions 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 us to modify each number in an array nums by either adding k or subtracting k. Our goal is to minimize the difference between the maximum and minimum values in the modified array. This difference is called the "score". A brute-force approach, checking all 2^n possibilities, is infeasible given the constraints.

Solution

Smallest Range II in Python

class Solution:
    def smallestRangeII(self, nums: List[int], k: int) -> int:
        n = len(nums)
        if n == 1:
            return 0

        # Sort the array to apply the partitioning strategy.
        nums.sort()

        # The initial result is the score if we add k to all elements or subtract k from all elements.
        # In both cases, the range remains the same.
        result = nums[n - 1] - nums[0]

        # Iterate through all possible split points.
        # For a split at index i, nums[0...i] get +k and nums[i+1...n-1] get -k.
        for i in range(n - 1):
            # After the split, the potential candidates for the new maximum are:
            # 1. The max of the first part: nums[i] + k
            # 2. The max of the second part: nums[n-1] - k
            high = max(nums[i] + k, nums[n - 1] - k)

            # The potential candidates for the new minimum are:
            # 1. The min of the first part: nums[0] + k
            # 2. The min of the second part: nums[i+1] - k
            low = min(nums[0] + k, nums[i + 1] - k)

            # Update the result with the minimum score found so far.
            result = min(result, high - low)

        return result

Smallest Range II in Java

class Solution {
    public int smallestRangeII(int[] nums, int k) {
        int n = nums.length;
        if (n == 1) {
            return 0;
        }

        // Sort the array to apply the partitioning strategy.
        Arrays.sort(nums);

        // The initial result is the score if we add k to all elements or subtract k from all elements.
        // In both cases, the range remains the same.
        int result = nums[n - 1] - nums[0];

        // Iterate through all possible split points.
        // For a split at index i, nums[0...i] get +k and nums[i+1...n-1] get -k.
        for (int i = 0; i < n - 1; i++) {
            // After the split, the potential candidates for the new maximum are:
            // 1. The max of the first part: nums[i] + k
            // 2. The max of the second part: nums[n-1] - k
            int high = Math.max(nums[i] + k, nums[n - 1] - k);
            
            // The potential candidates for the new minimum are:
            // 1. The min of the first part: nums[0] + k
            // 2. The min of the second part: nums[i+1] - k
            int low = Math.min(nums[0] + k, nums[i + 1] - k);
            
            // Update the result with the minimum score found so far.
            result = Math.min(result, high - low);
        }

        return result;
    }
}

Smallest Range II in C++

class Solution {
public:
    int smallestRangeII(vector<int>& nums, int k) {
        int n = nums.size();
        if (n == 1) {
            return 0;
        }

        // Sort the array to apply the partitioning strategy.
        sort(nums.begin(), nums.end());

        // The initial result is the score if we add k to all elements or subtract k from all elements.
        // In both cases, the range remains the same.
        int result = nums[n - 1] - nums[0];

        // Iterate through all possible split points.
        // For a split at index i, nums[0...i] get +k and nums[i+1...n-1] get -k.
        for (int i = 0; i < n - 1; ++i) {
            // After the split, the potential candidates for the new maximum are:
            // 1. The max of the first part: nums[i] + k
            // 2. The max of the second part: nums[n-1] - k
            int high = max(nums[i] + k, nums[n - 1] - k);
            
            // The potential candidates for the new minimum are:
            // 1. The min of the first part: nums[0] + k
            // 2. The min of the second part: nums[i+1] - k
            int low = min(nums[0] + k, nums[i + 1] - k);
            
            // Update the result with the minimum score found so far.
            result = min(result, high - low);
        }

        return result;
    }
};

Smallest Range II in TypeScript

function smallestRangeII(nums: number[], k: number): number {
    const n = nums.length;
    if (n === 1) {
        return 0;
    }

    // Sort the array to apply the partitioning strategy.
    nums.sort((a, b) => a - b);

    // The initial result is the score if we add k to all elements or subtract k from all elements.
    // In both cases, the range remains the same.
    let result = nums[n - 1] - nums[0];

    // Iterate through all possible split points.
    // For a split at index i, nums[0...i] get +k and nums[i+1...n-1] get -k.
    for (let i = 0; i < n - 1; i++) {
        // After the split, the potential candidates for the new maximum are:
        // 1. The max of the first part: nums[i] + k
        // 2. The max of the second part: nums[n-1] - k
        const high = Math.max(nums[i] + k, nums[n - 1] - k);

        // The potential candidates for the new minimum are:
        // 1. The min of the first part: nums[0] + k
        // 2. The min of the second part: nums[i+1] - k
        const low = Math.min(nums[0] + k, nums[i + 1] - k);

        // Update the result with the minimum score found so far.
        result = Math.min(result, high - low);
    }

    return result;
};

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.