Problem · Array
Minimum Operations to Make an Array Continuous
Learn this problemProblem 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
nelements 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[]) → intExamples
Example 1
nums = [4,2,5,3]return = 0The values are already the unique consecutive set {2,3,4,5}.
Example 2
nums = [1,2,3,5,6]return = 1Replace 1 with 4 to obtain the continuous set {2,3,4,5,6}.
Example 3
nums = [1,10,100,1000]return = 3At most one existing value can be retained inside any interval containing four consecutive integers, so three elements must be replaced.