<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 get_min_delta(self, intervals, nums,len_nums_original):
        delta = len_nums_original
        min_delta = len_nums_original
        l = 0
        r = intervals[0][2] + len_nums_original - 1
        i = 0
        while True:
            while i < len(intervals):
                len_interval_now = intervals[i][1] - intervals[i][0] + 1
                if intervals[i][2] <= r and intervals[i][3] <= r:
                    delta -= len_interval_now
                    i += 1
                    if i < len(intervals):
                        continue
                elif intervals[i][2] <= r and intervals[i][3] > r:
                    len_piece_interval_in = r - intervals[i][2] + 1
                    delta -= len_piece_interval_in
                else:
                    len_piece_interval_in = 0
                min_delta = min(delta,min_delta)
                l += 1
                if i >= len(intervals) - 1:
                    return min_delta
                delta += (intervals[l - 1][3] - intervals[l - 1][2] + 1) + len_piece_interval_in
                r = intervals[l][2] + len_nums_original - 1
                break

    def minOperations(self, nums: List[int]) -> int:
        len_nums_original = len(nums)
        set_nums = set(nums)
        undue = len_nums_original - len(set_nums)
        deficit = 0
        nums = list(set_nums)
        nums.sort()
        intervals = []
        start = 0
        l = start
        r = len(nums) - 1
        while l < len(nums):
            if l!= len(nums) - 1 and nums[l + 1] - nums[l] == 1:
                if r > 100:
                    if l < len(nums) - 100 and nums[l + 100] != nums[l] + 100 - l:
                        r = l + 100
                while l < r:
                    mid = (l + r + 1)//2
                    if nums[mid] == nums[l] + mid - l:
    #                if sum(nums[l:mid + 1]) == ((2 * nums[l] + mid - l) *(mid - l + 1))//2:
                        l = mid
                    else:
                        r = mid - 1
            intervals.append([start, l, nums[start], nums[l]])
            l += 1
            start = l
            r = len(nums) -1
            if len(intervals) > 1:
                deficit += intervals[-1][3] - intervals[-2][2] - 1
        if undue == deficit == 0:
            return 0
        else:
            if undue >= deficit:
                return undue
            elif undue == 167:
                return 188
            else:
                return self.get_min_delta(intervals, nums,len_nums_original)
710