Minimum Number of Operations to Make Array Continuous

You are given an integer array nums. In one operation, you can replace any element in nums with any integer.

nums is considered continuous if both of the following conditions are fulfilled:

  • All elements in nums are unique.
  • The difference between the maximum element and the minimum element in nums equals nums.length - 1.

Return the minimum number of operations to make nums continuous.

Example 1
Inputnums = [4,2,5,3]
Output0
nums is already continuous.
Example 2
Inputnums = [1,2,3,5,6]
Output1
One possible solution is to change the last element to 4, resulting in [1, 2, 3, 5, 4], which is continuous.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate