<p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>

<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>

<ul>
	<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
	<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>

<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>

<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong>&nbsp;nums is already continuous.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong>&nbsp;One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong>&nbsp;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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def minOperations(self, nums: List[int]) -> int:
        '''
        diff = 2
        [1, 2, 4]
        [1,2, 3] -> 1

        [1, 2, 2]
        [1,2,3] -> 1

        [2,2,2]
        [1,2,3] -> 2

        [4,4,4]
        [2,3,4] -> 2

        [5,5,5]
        [3,4,5] -> 2

        [1,10,100]
        [1, 2, 3] -> 2

        [1, 98, 100]
        [99, 98, 100] -> 1

        diff = 3
        [1, 98, 99, 100]
        
        diff = 9
        [18, 24, 26, 28, 29, 33, 33, 35, 41, 47]
        a1: 18 -> 27, 24 -> 30, 33 -> 31, 41 -> 32 and 47 -> 34
        consider each number as min
        18: [18, 27] -> 7
        24: [24, 33] -> 5
        26: [26, 35] -> 5
        ...
        47: [47, 56] -> 9
        '''

        N = len(nums)
        diff = N - 1

        s = list(set(nums))
        res = N
        s.sort()

        def inside_range(left, right):
            li = bisect_left(s, left) 
            ri = bisect_right(s, right) - 1
            return ri - li + 1

        for n in s:
            count = inside_range(n, n + diff)
            ops = N - count
            res = min(res, ops)

        return res




719