Minimum Moves to Make Array Complementary Using Difference Array ✅✅
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Range of Sums:\n\n    - Each pair of numbers nums[i] and nums[n - 1 - 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Range of Sums:\n\n    - Each pair of numbers nums[i] and nums[n - 1 - i] (where i ranges from 0 to n/2 - 1) can produce a sum between 2 (when both elements are 1) and 2 * limit (when both elements are limit).\n2. Calculate Moves:\n\n    - For each pair, determine the minimum and maximum possible sums they could achieve without any modification and note when modifications are necessary.\n    - Use a difference array to count the range of sums and the number of moves for each range. This keeps the complexity low while tracking how modifications affect potential target sums.\n3. Implement Difference Array:\n\n    - We initialize an array delta to record the number of moves for each possible sum.\n    - For each pair (a, b), calculate:\n        - The minimum sum achievable without any modification: a + b\n        - The range of sums that can be achieved with a single modification.\n    - Update the delta array accordingly.\n4. Calculate Minimum Moves:\n\n    - Using the delta array, perform a sweep to calculate the minimum number of moves for each sum within the allowed range.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```javascript []\n/**\n * @param {number[]} nums\n * @param {number} limit\n * @return {number}\n */\nvar minMoves = function(nums, limit) {\n    const n = nums.length;\n    const delta = Array(2 * limit + 2).fill(0); // to accommodate all sum ranges up to 2 * limit\n    \n    for (let i = 0; i < n / 2; i++) {\n        let a = nums[i];\n        let b = nums[n - 1 - i];\n        \n        delta[2] += 2;                       // Assume each pair needs two moves by default\n        delta[Math.min(a, b) + 1] -= 1;      // One move needed for sums between min(a, b) + 1\n        delta[a + b] -= 1;                   // Zero moves needed for sum a + b\n        delta[a + b + 1] += 1;               // One move needed for sums between a + b + 1\n        delta[Math.max(a, b) + limit + 1] += 1; // Two moves needed beyond max(a, b) + limit\n    }\n    \n    let minMoves = Infinity;\n    let currentMoves = 0;\n    \n    for (let sum = 2; sum <= 2 * limit; sum++) {\n        currentMoves += delta[sum];\n        minMoves = Math.min(minMoves, currentMoves);\n    }\n    \n    return minMoves;\n};\n\n```
['JavaScript']