Question

Formatted question description: https://leetcode.ca/all/1674.html

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

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

Return the minimum number of moves required to make nums complementary.


Example 1:

Input: nums = [1,2,4,3], limit = 4
Output: 1
Explanation: In 1 move, you can change nums to [1,2,2,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.


Example 2:

Input: nums = [1,2,2,1], limit = 2
Output: 2
Explanation: In 2 moves, you can change nums to [2,2,2,2]. You cannot change any number to 3 since 3 > limit.


Example 3:

Input: nums = [1,2,1,2], limit = 2
Output: 0
Explanation: nums is already complementary.


Constraints:

n == nums.length
2 <= n <= 105
1 <= nums[i] <= limit <= 105
n is even.

Algorithm

We think according to the piecewise function, assuming that a and b are a pair, that is, nums[i]=a, nums[n-i-1]=b.

  • First, if nothing is changed, that is, 0 times, we get a+b.
  • If you change it once and make it bigger, then you should change a to make it as large as possible to get limit+b
  • if you change it to smaller, then you should change b to make it smaller, and the minimum is 1 to get 1+a .
  • If you change it twice, if you change it to a larger value, you can at least get limit+b+1, that is, a becomes larger and b becomes 1 larger than itself;
  • similarly, if you change it to a smaller value, you can get the minimum of 2.

So we got the key points:

  • [2, a], need to change 2 times;
  • [a+1, a+b-1], need to change 1 time;
  • a+b, need to change 0 times;
  • [a+b+1, limit+b], need Change it once;
  • finally [limit+b+1, 2limit] needs to be changed twice. Here we can think that 2limit is the maximum value because the limit given in constraints is always greater than or equal to nums[i].

Therefore, the important point is 2, a+1, a+b, a+b+1, limit+b+1, using the difference, assuming that the original initial number of changes is 0, starting from the above point, the number of changes is +2 (2), -1(1), -1(0), +1(1), +1(2), the actual number of changes to be changed in the brackets, thus forming a piecewise function. Open an array of size limit*2+2, and then for n/2 pairs of points, we calculate these points, and perform + or-operations on these points on the array. Calculate the prefix sum of this array and find the smallest moment, which is the answer.

Code

Java

public class Minimum_Moves_to_Make_Array_Complementary {

    class Solution {
        public int minMoves(int[] nums, int limit) {

            int n = nums.length;
            int[] ps = new int[limit * 2 + 2];

            for (int i = 0; i < n / 2; i++) {
                int v1 = nums[i], v2 = nums[n - 1 - i];
                ps[1 + Math.min(v1, v2)]--;
                ps[v1 + v2]--;
                ps[v1 + v2 + 1]++;
                ps[Math.max(v1, v2) + limit + 1]++;
            }

            int ans = n;
            int curr = n;
            for (int i = 2; i <= limit * 2; i++) {
                curr += ps[i];
                ans = Math.min(ans, curr);
            }
            return ans;
        }
    }

}

Java

class Solution {
    public int minMoves(int[] nums, int limit) {
        int sumLimit = limit * 2;
        int[] differences = new int[sumLimit + 1];
        for (int i = 0, j = nums.length - 1; i < j; i++, j--) {
            int sum = nums[i] + nums[j];
            int curMinSum = Math.min(nums[i], nums[j]) + 1;
            int curMaxSum = Math.max(nums[i], nums[j]) + limit;
            differences[curMinSum]--;
            differences[sum]--;
            if (sum < sumLimit)
                differences[sum + 1]++;
            if (curMaxSum < sumLimit)
                differences[curMaxSum + 1]++;
        }
        int maxReduce = 0;
        for (int i = 1; i <= sumLimit; i++) {
            differences[i] += differences[i - 1];
            maxReduce = Math.min(maxReduce, differences[i]);
        }
        return nums.length + maxReduce;
    }
}

All Problems

All Solutions