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

        """
        - minimum_x_to_plus_two
            - curr >= other, or other > curr
            - We want to maximize the abs(curr - other) to cover more 'x' to minimise +2s -> substitute k or 0.
            - if curr >= other -> max(k - other, curr)
            - if curr < other -> max(k - curr, other)
            - max( max(k - other, curr), max(k - curr, other) ) = max(k - other, curr, k - curr, other) 
        """



        n = len(nums)
        max_x = k + 1

        diffs = defaultdict(int)
        counter = defaultdict(int)
        for idx in range(len(nums) // 2):
            curr, other = nums[idx], nums[n-idx-1]
            diffs[abs(curr - other)] += 1
            minimum_x_to_plus_two = max(k - other, k - curr, curr, other) + 1 # if x == minimum_x_to_plus_two, need to plus 2.
            counter[minimum_x_to_plus_two] += 1
    
        res = n//2
        running_plus_twos = 0
        for x in range(max_x):
            running_plus_twos += counter[x] 

            skip = diffs[x] # ==, skip
            plus_one = n//2 - running_plus_twos - skip
            res = min(res, running_plus_twos*2 + plus_one)
        return res
            





        # diffs = [abs(nums[idx]-nums[n-idx-1]) for idx in range(n)]
        # print(diffs)


        # counter = {x: 0 for x in range(k + 1)}
        # for idx in range(len(nums) // 2):
        #     curr, other = nums[idx], nums[n-idx-1]
        #     x = max(k - min(curr, other), max(curr, other))
        #     counter[x] += 1 # at this x, counter[x] diffs will have to +2. so all diff < x have to += 2 if we choose this x.
        
        # print(counter)

        # # out of all x, which x is the best?
        # # x is the best when we minimize the prefix sum (+ 2) + suffix_sum (+1)

        # candidates = sorted(counter.keys())
        # suffix_sum = deque([0])
        # for idx in range(len(candidates) - 2, -1, -1):
        #     suffix_sum.appendleft(suffix_sum[0] + counter[candidates[idx+1]])

        # prefix_sum = [0]
        # for idx in range(1, len(candidates)):
        #     prefix_sum.append(prefix_sum[-1] + counter[candidates[idx-1]])

        # # counter = 0
        # # for x in candidates:
        # #     counter += 2 * counter[x]
        
        # res = n
        # print(prefix_sum)
        # print(list(suffix_sum))

        # # diffs = [abs(nums[idx]-nums[n-idx-1]) for idx in range(n//2)]
        # for x in range(k + 1): 
        #     changes = 0
        #     for idx in range(len(nums) // 2):
        #         if abs(nums[idx] - nums[n - idx - 1]) == x: continue

        #         curr, other = nums[idx], nums[n-idx-1]
        #         if k - min(curr, other) < x and max(curr, other) < x: # substitute one for k or 0
        #             changes += 2
        #         else:
        #             changes += 1



        #     res = min(res, changes)
        #     print(res, x)
        # return res

42400