FastPrepMinimize Range After Three Changes

Minimize Range After Three Changes

DRW logoDRW● MediumINTERNPHONE SCREEN
Learn

Problem statement

You are given an integer array nums. You may change the value of at most three elements to any integers.

Return the minimum possible difference between the largest and smallest values after the changes.

Function

minimizeRange(nums: int[]) → int

Examples

Example 1

nums = [5,3,2,4]return = 0

Change three values to match the remaining value, so the range becomes 0.

Example 2

nums = [1,5,6,14,15]return = 1

Keep 5 and 6 and change the other three elements.

Example 3

nums = [6,6,0,1,1,4,6]return = 2

Changing 0, both 1s, or an equivalent set leaves values between 4 and 6.

Constraints

  • 1 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000

More DRW problems

See DRW hiring insights
public int minimizeRange(int[] nums) {
    // Write your code here.
}
nums[5,3,2,4]
expected0
Checking account…