<p>You are given an integer array <code>nums</code> of <strong>even</strong> length <code>n</code> and an integer <code>limit</code>. In one move, you can replace any integer from <code>nums</code> with another integer between <code>1</code> and <code>limit</code>, inclusive.</p>

<p>The array <code>nums</code> is <strong>complementary</strong> if for all indices <code>i</code> (<strong>0-indexed</strong>), <code>nums[i] + nums[n - 1 - i]</code> equals the same number. For example, the array <code>[1,2,3,4]</code> is complementary because for all indices <code>i</code>, <code>nums[i] + nums[n - 1 - i] = 5</code>.</p>

<p>Return the <em><strong>minimum</strong> number of moves required to make </em><code>nums</code><em> <strong>complementary</strong></em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,4,3], limit = 4
<strong>Output:</strong> 1
<strong>Explanation:</strong> In 1 move, you can change nums to [1,2,<u>2</u>,3] (underlined elements are changed).
nums[0] + nums[3] = 1 + 3 = 4.
nums[1] + nums[2] = 2 + 2 = 4.
nums[2] + nums[1] = 2 + 2 = 4.
nums[3] + nums[0] = 3 + 1 = 4.
Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,2,1], limit = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> In 2 moves, you can change nums to [<u>2</u>,2,2,<u>2</u>]. You cannot change any number to 3 since 3 &gt; limit.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,1,2], limit = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums is already complementary.
</pre>

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

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

class Solution:
    def minMoves(self, nums, limit):
        pairs_count = len(nums) // 2  # Store the number of pairs
        delta = [0] * (limit * 2 + 2)  # Delta array to store the cost changes

        # Iterate over each pair
        for i in range(pairs_count):
            # Taking the minimum and maximum of the pair for optimization
            min_of_pair = min(nums[i], nums[len(nums) - i - 1])
            max_of_pair = max(nums[i], nums[len(nums) - i - 1])

            # Always needs 2 moves if sum is larger than max possible sum 'limit * 2'
            delta[2] += 2
            delta[limit * 2 + 1] -= 2

            # If we make minOfPair + 1 the new sum, we would need one less move
            delta[min_of_pair + 1] -= 1
            # If the sum is greater than maxOfPair + limit, then we'll need an additional move
            delta[max_of_pair + limit + 1] += 1

            # We need one less move for making the sum equal to minOfPair + maxOfPair
            delta[min_of_pair + max_of_pair] -= 1
            delta[min_of_pair + max_of_pair + 1] += 1

        min_moves = pairs_count * 2  # Initialize minMoves to the maximum possible value
        current_cost = 0  # Variable to accumulate current cost

        # Iterate over possible sums
        for sum_value in range(2, limit * 2 + 1):
            current_cost += delta[sum_value]  # Update current cost
            # If current cost is less than minMoves, update minMoves
            if current_cost < min_moves:
                min_moves = current_cost

        return min_moves  # Return the minimum moves required

30700