FastPrepMinimum Operations to Make an Array Continuous
Problem · Array

Minimum Operations to Make an Array Continuous

Learn this problem
HardAmazon logoAmazonNEW GRADONSITE INTERVIEW
See Amazon hiring insights

Problem statement

You are given a non-empty integer array nums of length n. In one operation, you may replace any one element with any integer.

An array is continuous when both conditions hold:

  • All n elements are unique.
  • The difference between the maximum and minimum elements is exactly n - 1.

Return the minimum number of replacement operations needed to make nums continuous.

Function

minOperations(nums: int[]) → int

Examples

Example 1

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

The values are already the unique consecutive set {2,3,4,5}.

Example 2

nums = [1,2,3,5,6]return = 1

Replace 1 with 4 to obtain the continuous set {2,3,4,5,6}.

Example 3

nums = [1,10,100,1000]return = 3

At most one existing value can be retained inside any interval containing four consecutive integers, so three elements must be replaced.

More Amazon problems

drafts saved locally
public int minOperations(int[] nums) {
  // write your code here
}
nums[4,2,5,3]
expected0
checking account