LeetCode solutions

1793. Minimum Moves to Make Array Complementary

My accepted Python solution to LeetCode problem 1793, Minimum Moves to Make Array Complementary, running in 319ms.

  • Difficulty: Medium
  • Python
  • Runtime 319ms
  • Memory 28.8MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 319ms, memory 28.8MB, accepted 2026-01-01.

python
class Solution:
    def minMoves(self, nums: List[int], limit: int) -> int:
        n = len(nums)
        # diff array for difference array technique
        diff = [0] * (2 * limit + 2)
        
        for i in range(n // 2):
            a, b = nums[i], nums[n - 1 - i]
            lo = min(a, b) + 1
            hi = max(a, b) + limit
            
            # For target in [2, lo-1] and [hi+1, 2*limit]: need 2 moves
            diff[2] += 2
            diff[2 * limit + 1] -= 2
            
            # For target in [lo, hi]: need 1 move
            diff[lo] -= 1
            diff[hi + 1] += 1
            
            # For target = a + b: need 0 moves
            diff[a + b] -= 1
            diff[a + b + 1] += 1
        
        result = float('inf')
        curr = 0
        for t in range(2, 2 * limit + 1):
            curr += diff[t]
            result = min(result, curr)
        
        return result

Source