<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p>

<p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in the range from <code>0</code> to <code>k</code>.</p>

<p>You need to perform some changes (possibly none) such that the final array satisfies the following condition:</p>

<ul>
	<li>There exists an integer <code>X</code> such that <code>abs(a[i] - a[n - i - 1]) = X</code> for all <code>(0 &lt;= i &lt; n)</code>.</li>
</ul>

<p>Return the <strong>minimum</strong> number of changes required to satisfy the above condition.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,0,1,2,4,3], k = 4</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong><br />
We can perform the following changes:</p>

<ul>
	<li>Replace <code>nums[1]</code> by 2. The resulting array is <code>nums = [1,<u><strong>2</strong></u>,1,2,4,3]</code>.</li>
	<li>Replace <code>nums[3]</code> by 3. The resulting array is <code>nums = [1,2,1,<u><strong>3</strong></u>,4,3]</code>.</li>
</ul>

<p>The integer <code>X</code> will be 2.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,2,3,3,6,5,4], k = 6</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong><br />
We can perform the following operations:</p>

<ul>
	<li>Replace <code>nums[3]</code> by 0. The resulting array is <code>nums = [0,1,2,<u><strong>0</strong></u>,3,6,5,4]</code>.</li>
	<li>Replace <code>nums[4]</code> by 4. The resulting array is <code>nums = [0,1,2,0,<strong><u>4</u></strong>,6,5,4]</code>.</li>
</ul>

<p>The integer <code>X</code> will be 4.</p>
</div>

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

<ul>
	<li><code>2 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>n</code> is even.</li>
	<li><code>0 &lt;= nums[i] &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def minChanges(self, nums: List[int], k: int) -> int:
        # - let's split out and reverse the second half of the list so have 
        # - we have two arrays nums1, nums2 we need to make abs(nums1[i] - nums2[i]) = X 
        #   for some X 
        # nums[i] <= k
        # we probably should pick X to be a diff we have already??

        # we need to decide X, 2 cases?
        # naive approach
        # take X = most frequent of abs(nums1[0] - nums2[0]), abs(nums1[1] - nums2[1] ....
        # adjust one or both of the other pairs
        # this will go wrong if X is high and lots of nums1, nums2 are close to each other
        # given X can we count how many bad pairs there are quickly
        # if n2 - n1 != X and n1 < X and n2 >= N - X X >= 
        # if we take X = 0 we should be able to do in len(nums) // 2 
        # otherwise see if any of the other current diffs work better?

        # n1, n2 can be done in 1 move if X <= max(n1, N - n2 -1) so order list by this value

        n = len(nums)
        h = n // 2
        nums1 = nums[:h]
        nums2 = list(reversed(nums[h:]))

        max_Xs = []
        count_current_X = Counter()
        for n1, n2 in zip(nums1, nums2):
            if n1 > n2:
                n1, n2 = n2, n1

            # we can move either n1 or n2 anywhere in [0, k]
            # can't solve in one move if both of:
            #.   - n2 > X -> n1 > X
            #.   - k - n1 <= X -> k - n2 <= X
            max_X = max(n2, k - n1) 
            max_Xs.append(max_X)
            count_current_X[n2 - n1] += 1

        # 0,1,2,3   4,5,6,3 
        # max_Xs = 3, 5, 6, 6
        # counts = (3, 1), (4, 3)

        max_Xs = sorted(max_Xs, reverse=True) # stack

        out = h - count_current_X[0] # (X = 0 case)
        
        current_bad = 0

        for x, cnt in sorted(count_current_X.items()):
            while max_Xs and max_Xs[-1] < x:
                max_Xs.pop()
                current_bad += 1 # bad pair means we have to add extra 1 
            
            out = min(out, h + current_bad - cnt)

        return out



        
32100