<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:
        n = len(nums)
        n_diffs = n // 2

        # Find the counts for the symetrical differences.        
        diff_counts = defaultdict(int)
        for i in range(n_diffs):
            diff = abs(nums[i] - nums[~i])
            diff_counts[diff] += 1

        # Find the counts for the cost 1 thresholds
        """
        nums[i] = 3, nums[~i] = 8, k = 10 の場合:

        1. l = 3, h = 8
        2. cost_1_threshold = max(8, 10-3) = max(8, 7) = 8
        
        この場合、1回の変更で0から8までの任意の差を作ることができます。
        しかし、9以上の差を作るには、必ず2回の変更が必要になります。
        """
        cost_1_threshold_counts = defaultdict(int)
        for i in range(n_diffs):
            l = min(nums[i], nums[~i])
            h = max(nums[i], nums[~i])
            cost_1_threshold = max(h, k-l)
            cost_1_threshold_counts[cost_1_threshold] += 1

        # Calculate the cost for each possible value of x.
        min_cost = float('inf')
        n_cost_2 = 0 # => 差をゼロにするための2回操作回数は必ずゼロから始める　 # => left, right のどちらかをもう片方にすれば良いから
        for x in range(k+1): # => 全ての差 x に対して最小値を求めている
            cur_cost = n_diffs + n_cost_2 - diff_counts[x] 

            min_cost = min(min_cost, cur_cost)

            n_cost_2 += cost_1_threshold_counts[x] # => しきい値がズレるので徐々に1回操作回数が2回操作回数になる
        
        return min_cost
42500