The Minimum Number of Operations to Make Array Continuous interview question is a "Hard" difficulty problem involving sorting and range management. An array of length n is "continuous" if all its elements are unique and the difference between the maximum and minimum element is n-1. You can replace any element with any other integer. Your goal is to find the minimum number of replacements to make the array continuous.
Uber and Microsoft use this to test a candidate's ability to rephrase an optimization problem. The core question is: "What is the maximum number of elements from the original array that can be part of a continuous range?". Once you find that maximum (let's call it max_keep), the answer is simply n - max_keep.
This utilizes the Array, Hash Table, Sliding Window, Binary Search interview pattern.
array[i], imagine it is the start of the continuous range: [array[i], array[i] + n - 1].n - max.Original: [4, 2, 5, 3], n=4 Sorted unique: [2, 3, 4, 5]
Original: [1, 2, 3, 5, 6], n=5 Sorted unique: [1, 2, 3, 5, 6]
A common mistake is not removing duplicates before processing, which leads to incorrect counts of how many unique elements fit in the n-1 span. Another error is trying to find the range using O(n^2) brute force instead of an O(n log n) binary search or O(n) sliding window.
When asked for the "minimum changes to satisfy a range property," try to find the "maximum elements that already satisfy the property." This inversion of the problem often leads to much cleaner logic involving sorting and searching.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find the Median of the Uniqueness Array | Hard | Solve | |
| Find the Longest Equal Subarray | Medium | Solve | |
| Find Two Non-overlapping Sub-arrays Each With Target Sum | Medium | Solve | |
| Minimum Operations to Reduce X to Zero | Medium | Solve | |
| Maximum Erasure Value | Medium | Solve |