Fewest replacements to turn an array into distinct consecutive integers

Quick Overview

Find the fewest element replacements that turn an integer array into a set of distinct consecutive integers. It tests sorting with deduplication, a two-pointer sliding window over values, and the argument for why only existing values need to be tried as the start of the range.

Fewest replacements to turn an array into distinct consecutive integers

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

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 hold: - All elements in `nums` are unique. - The difference between the maximum element and the minimum element in `nums` equals `len(nums) - 1`. In other words, `nums` is continuous if it can be rearranged to form a sequence of consecutive integers. Return the minimum number of operations to make `nums` continuous. ### Function Signature ```python def min_operations(nums: list[int]) -> int: ``` ### Rules - An operation changes exactly one element. The new value can be any integer, including one outside the range of the original values. - The length of the array never changes, so a continuous result of length `n` holds exactly the integers `x, x + 1, ..., x + n - 1` for some `x`. - An array that is already continuous needs 0 operations. ### Constraints - `1 <= len(nums) <= 10^5` - `1 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [4, 2, 5, 3] Output: 0 ``` The values 2, 3, 4, 5 are distinct, and 5 - 2 = 3 = 4 - 1, so the array is already continuous. ```text Input: nums = [1, 2, 3, 5, 6] Output: 1 ``` The array is not continuous yet, because 6 - 1 = 5 and the length is 5. The values 1, 2, 3, 5 all fit in the range 1 to 5, so only 6 has to change: replacing it with 4 gives `[1, 2, 3, 5, 4]`. ```text Input: nums = [1, 2, 2, 2, 10] Output: 3 ``` Only distinct values can be kept, and 10 is too far from the others to share a range of five consecutive integers with them. The best choice keeps 1 and one 2, then replaces the other two 2s and the 10 with 3, 4 and 5.

Overview: Find the fewest element replacements that turn an integer array into a set of distinct consecutive integers. It tests sorting with deduplication, a two-pointer sliding window over values, and the argument for why only existing values need to be tried as the start of the range.

Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

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 hold:

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

In other words, nums is continuous if it can be rearranged to form a sequence of consecutive integers.

Return the minimum number of operations to make nums continuous.

Function Signature

def min_operations(nums: list[int]) -> int:

Rules

  • An operation changes exactly one element. The new value can be any integer, including one outside the range of the original values.
  • The length of the array never changes, so a continuous result of length n holds exactly the integers x, x + 1, ..., x + n - 1 for some x .
  • An array that is already continuous needs 0 operations.

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= nums[i] <= 10^9

Examples

Input: nums = [4, 2, 5, 3]
Output: 0

The values 2, 3, 4, 5 are distinct, and 5 - 2 = 3 = 4 - 1, so the array is already continuous.

Input: nums = [1, 2, 3, 5, 6]
Output: 1

The array is not continuous yet, because 6 - 1 = 5 and the length is 5. The values 1, 2, 3, 5 all fit in the range 1 to 5, so only 6 has to change: replacing it with 4 gives [1, 2, 3, 5, 4].

Input: nums = [1, 2, 2, 2, 10]
Output: 3

Only distinct values can be kept, and 10 is too far from the others to share a range of five consecutive integers with them. The best choice keeps 1 and one 2, then replaces the other two 2s and the 10 with 3, 4 and 5.