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:
nums are unique.nums equals nums.length - 1.For example, nums = [4, 2, 5, 3] is continuous, but nums = [1, 2, 3, 5, 6] is not continuous.
Return the minimum number of operations to make nums continuous.
Example 1:
Input: nums = [4,2,5,3] Output: 0 Explanation: nums is already continuous.
Example 2:
Input: nums = [1,2,3,5,6] Output: 1 Explanation: One possible solution is to change the last element to 4. The resulting array is [1,2,3,5,4], which is continuous.
Example 3:
Input: nums = [1,10,100,1000] Output: 3 Explanation: One possible solution is to: - Change the second element to 2. - Change the third element to 3. - Change the fourth element to 4. The resulting array is [1,2,3,4], which is continuous.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To figure out the minimum number of changes needed to make a collection of numbers consecutive, the brute force approach involves testing every single possible sequence of consecutive numbers that could theoretically be formed. We then count how many changes each of those possibilities would require.
Here's how the algorithm would work step-by-step:
def min_operations(nums):
total_items = len(nums)
minimum_operations = total_items
# We test every unique number in the array as the potential starting point of our sequence
for starting_number in nums:
target_sequence_end = starting_number + total_items - 1
current_missing_count = 0
for sequence_element in range(starting_number, target_sequence_end + 1):
if sequence_element not in nums:
current_missing_count += 1
# We track the smallest number of missing elements found across all tested target sequences
if current_missing_count < minimum_operations:
minimum_operations = current_missing_count
return minimum_operationsTo make a collection of numbers continuous and unique, we can visualize the goal as sliding a fixed-size window of consecutive numbers across all the unique values we were given. The optimal strategy finds the position where this window already captures the maximum possible numbers, meaning we only need to change the remaining numbers that fall outside the window.
Here's how the algorithm would work step-by-step:
def min_operations(nums):
total_elements_count = len(nums)
# Removing duplicates ensures all numbers in our sequence are strictly unique.
sorted_unique_numbers = sorted(list(set(nums)))
max_captured_elements = 0
window_start_index = 0
for window_end_index in range(len(sorted_unique_numbers)):
# We shift the window left boundary until the span fits the total element count.
while sorted_unique_numbers[window_end_index] - sorted_unique_numbers[window_start_index] >= total_elements_count:
window_start_index += 1
current_window_size = (window_end_index - window_start_index) + 1
max_captured_elements = max(max_captured_elements, current_window_size)
# Subtracting the max retained elements from total gives the minimum replacements needed.
return total_elements_count - max_captured_elements| Case | How to Handle |
|---|---|
| Array with a single element | Return zero because a single-element array is already continuous by definition. |
| Array where all elements are already distinct and form a continuous range | Return zero because no operations are needed since the condition is already met. |
| Array containing all identical elements | Deduplicate the array and return length minus one since only one unique element can be kept. |
| Array containing negative numbers and zeros | Sorting and sliding window naturally handle negative ranges and zero crossings without extra offset logic. |
| Array with elements already sorted in ascending order | The sliding window efficiently scans the sorted unique elements without requiring a preliminary sort step overhead. |
| Array with extremely large values spanning beyond standard integer limits | Use 64-bit integers for window difference calculations to prevent potential overflow during subtraction. |
| Maximum-sized inputs up to ten to the fifth elements | Apply an O(N log N) sorting plus O(N) sliding window approach to ensure it executes well within time limits. |
| Array with heavily duplicated values leaving very few unique elements | Deduplicating the array first ensures the sliding window only evaluates valid unique starting points. |