Problem statement

https://binarysearch.com/problems/Equivalent-Folded-Sums/

Solution

Equal to Leetcode 1674 Minimum Moves to Make Array Complementary.

Complexity

Time complexity O(n + k), where k is our limit and n is length of nums. Space complexity is O(k).

Code

class Solution:
    def solve(self, nums):
        n = len(nums)//2
        k = max(nums)
        diffs = [0]*(2*k+2)
        for i in range(n):
            a, b = nums[i], nums[-1-i]
            diffs[min(a,b) + 1] += 1
            diffs[max(a,b) + k+1] -= 1
            diffs[a+b] += 1
            diffs[a+b+1] -= 1

        return 2*n - max(accumulate(diffs))