508. Minimum Number of Operations to Make Array Continuous

MediumBinary SearchBinary SearchSliding Window

An array is continuous if all its elements are distinct and the difference between the maximum and minimum equals n-1 (so the values form a run of consecutive integers). In one operation you may replace any element with any integer. Return the minimum operations to make the array continuous. Input: an array of integers.

Input: An array of integers.

Output: Integer — the minimum operations.

Examples

Example 1
Input: [4,2,5,3]
Output: 0
Explanation: Already continuous.
Example 2
Input: [1,2,3,5,6]
Output: 1
Explanation: Replace one element.
Example 3
Input: [1,10,100,1000]
Output: 3
Explanation: Keep one, replace three.

Constraints

Asked by

MicrosoftGoogleBloombergAmazon
Solve this problem in the editor →