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

# The key idea here: (Wow I did not think about this!)

# Consider the possibility of using EACH element as the MINIMUM in the continuous subarray!
# Since the RANGE of the array is fixed, e.g. if min = 2, len(arr) = 7, then 
#   in the end, the numbers in arr MUST be 2 ~ 8
# e.g. [2,4,7,8,9,10,12]
# If we use 2 as min, my range is 2-8, now do bisect, there are 3 numbers OUTSIDE this range (9,10,12)
#   meaning that they will DEFINITELY be replaced into numbers in 2-8 -> Min = 3 steps
# If use 4 as min, range is 4-10, now there are 2 numbers outside this range (2, 12) -> min = 2 steps

# Also, since there can be duplicates, numbers outside = len(nums) - EFFECTIVE, UNIQUE nums count inside range!
# e.g. [4,5,8,8,9,9] -> duplicates

import bisect

class Solution:
    def minOperations(self, nums: List[int]) -> int:
        # need to ensure "nums" contain unique elements!
        n = len(nums)
        unique_nums = set(nums)
        unique_nums = list(unique_nums)
        unique_nums.sort()

        minSteps = float("inf")
        for i in range(len(unique_nums)):
            # the two side of the final range, provided using this as min
            leftPtr = unique_nums[i]
            rightPtr = unique_nums[i] + n - 1 # Here should still be n-1, not length of unique nums!
            # print(f"left is {leftPtr}, right is {rightPtr}, with unique_nums {unique_nums}")

            leftOut = bisect.bisect_left(unique_nums, leftPtr)
            rightOut = n - bisect.bisect_right(unique_nums, rightPtr)

            # print(f"leftOut {leftOut}, rightOut {rightOut}")

            minSteps = min(minSteps, leftOut + rightOut)
            # print(minSteps)

        return minSteps







        
736