Quick Overview

Given an integer array, you may change up to three elements to any values; return the smallest possible difference between the largest and smallest values afterward. It tests reasoning about which extreme values are worth changing and careful handling of short arrays.

Minimize the Max-Min Gap of an Array After Changing Up to Three Elements

Company: DRW

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

You are given an integer array `nums`. You may change the values of at most three of its elements, setting each changed element to any integer you like. Return the smallest possible difference between the largest and the smallest value in the array after the changes. ### Function Signature ```python def min_range_after_three_changes(nums: list[int]) -> int: ``` ### Rules - Choose up to three positions and set each one to any integer, independently. Changing fewer than three elements, or none, is allowed. - Elements are changed, never removed: the array keeps its length. - Return the minimum, over all allowed changes, of `max(nums) - min(nums)` for the modified array. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^9 <= nums[i] <= 10^9` ### Examples **Example 1** ```text Input: nums = [9, 2, 7, 4] Output: 0 ``` Change `9`, `2` and `7` to `4`; every value is then equal. **Example 2** ```text Input: nums = [20, 75, 81, 82, 95] Output: 1 ``` Change `20`, `75` and `95` to `81`. The array becomes `[81, 81, 81, 82, 81]`, whose largest and smallest values differ by `1`. No set of three changes can make all five values equal, because `81` and `82` cannot both stay. **Example 3** ```text Input: nums = [6, 6, 0, 1, 1, 4, 6] Output: 2 ``` Change `0`, `1` and `1` to `6`. The array becomes `[6, 6, 6, 6, 6, 4, 6]`, and `6 - 4 = 2`.

Overview: Given an integer array, you may change up to three elements to any values; return the smallest possible difference between the largest and smallest values afterward. It tests reasoning about which extreme values are worth changing and careful handling of short arrays.

Read the full DRW Software Engineer interview experience this question came from

You are given an integer array `nums`. You may change the values of at most three of its elements, setting each changed element to any integer you like. Return the smallest possible difference between the largest and the smallest value in the array after the changes. Implement `min_range_after_three_changes(nums)`. ### Rules - Choose up to three positions and set each one to any integer, independently. Changing fewer than three elements, or none, is allowed. - Elements are changed, never removed: the array keeps its length. - Return the minimum, over all allowed changes, of `max(nums) - min(nums)` for the modified array. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^9 <= nums[i] <= 10^9` The answer is an integer between `0` and `2 * 10^9`. The Java and C++ signatures return a 64-bit integer (`long` / `long long`). ### Examples **Example 1** ```text Input: nums = [9, 2, 7, 4] Output: 0 ``` Change `9`, `2` and `7` to `4`; every value is then equal. **Example 2** ```text Input: nums = [20, 75, 81, 82, 95] Output: 1 ``` Change `20`, `75` and `95` to `81`. The array becomes `[81, 81, 81, 82, 81]`, whose largest and smallest values differ by `1`. No set of three changes can make all five values equal, because at least two of the five distinct values must stay unchanged.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

Input: ([5],)

Expected Output: 0

Explanation: Singleton: a single value already has range 0.

Input: ([-1000000000, 1000000000],)

Expected Output: 0

Explanation: Length 2 at both value extremes: change one element to equal the other.

Hints

  1. A changed element can be set to any integer, so it never has to fall outside the range of the elements you leave unchanged.
  2. Changing an element never makes the range larger, so think about how few values must stay unchanged, especially for very short arrays.
  3. Ask which of the original values could still be the largest or the smallest after the changes.

Loading coding console...

Show the approach

Approach

A changed element may take any integer, so it can always be set to a value inside the range of the unchanged elements. Changed elements therefore never decide the final maximum or minimum, and the answer is the smallest range achievable by the elements left unchanged. Changing more elements never makes that range larger, so with n <= 4 at most one element has to stay and the answer is 0. For n >= 5, change exactly three elements and keep n - 3. Sort a copy of the array into a. If a kept set has smallest sorted index lo and largest sorted index hi, its range is a[hi] - a[lo]; because it holds n - 3 elements, hi >= lo + n - 4, so the contiguous window a[lo..lo + n - 4] is also a valid kept set and its range is no larger. Some optimal kept set is therefore a window of n - 3 consecutive sorted values. There are exactly four such windows, one for each way to change the i smallest and the 3 - i largest values (i = 0..3), with range a[n - 4 + i] - a[i]. The answer is the minimum of these four. Duplicates need no special handling because equal values sit next to each other after sorting; zero and negative values behave like any other integer; the largest possible answer is 2 * 10^9, which the Java and C++ references return as a 64-bit integer.

Time complexity:
O(n log n)
Space complexity:
O(n)