> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Number of Operations to Make (#2009)

> Tested Python solution for LeetCode 2009 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 2009, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Binary Search](/catalog/topics/binary-search), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-continuous/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2009   # by problem number
lcpy gen -s minimum_number_of_operations_to_make_array_continuous   # by problem name
```

## Problem

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`.

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*.

### Examples

```
Input: nums = [4,2,5,3]
Output: 0
Explanation: nums is already continuous.
```

```
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.
```

```
Input: nums = [1,10,100,1000]
Output: 3
Explanation: One possible solution is to change the last three elements to 2, 3 and 4.
The resulting array is [1,2,3,4], which is continuous.
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_operations_to_make_array_continuous/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_operations_to_make_array_continuous/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def min_operations(self, nums: list[int]) -> int:
        n = len(nums)
        vals = sorted(set(nums))
        best = 0
        left = 0
        for right in range(len(vals)):
            while vals[right] - vals[left] > n - 1:
                left += 1
            best = max(best, right - left + 1)
        return n - best
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.