1674. Minimum Moves to Make Array Complementary

1674. Minimum Moves to Make Array Complementary #

Problem #

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 inums[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.

Problem Summary #

Given an integer array nums of even length n and an integer limit. In each operation, you can replace any integer in nums with another integer between 1 and limit.

If for all indices i (0-indexed), nums[i] + nums[n - 1 - i] are all equal to the same number, then the array nums is complementary. 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 operations required to make the array complementary.

Solution Approach #

  • This problem tests the difference array. By analyzing the problem statement, we can see that for each possible value of sum, the range is [2, 2* limt]. Define a = min(nums[i], nums[n - i - 1]), b = max(nums[i], nums[n - i - 1]). Within this interval, it can be further divided into 5 intervals: [2, a + 1), [a + 1, a + b), [a + b + 1, a + b + 1), [a + b + 1, b + limit + 1), [b + limit + 1, 2 * limit). In these 5 intervals, the minimum numbers of operations needed to make the array complementary are respectively 2(decrease a, decrease b), 1(decrease b), 0(no operation), 1(increase a), +2(increase a, increase b). Put another way, if we scan from left to right with a sweep line, the accumulated changes in the minimum number of operations needed to make the array complementary in these 5 intervals are respectively +2(decrease a, decrease b), -1(decrease a), -1(no operation), +1(increase a), +1(increase a, increase b). Using the relationship between adjacent intervals, we can construct a difference array. The difference array reflects the relationship between neighboring values. If we want to obtain the total relationship from 0 to n, we only need to compute the prefix sum once.
  • This problem requires outputting the minimum number of operations, so use a difference array + prefix sum, and maintain the minimum value while accumulating the prefix sum. After scanning once from left to right, output the minimum value.

Code #

package leetcode

func minMoves(nums []int, limit int) int {
	diff := make([]int, limit*2+2) // nums[i] <= limit, b+limit+1 is maximum limit+limit+1
	for j := 0; j < len(nums)/2; j++ {
		a, b := min(nums[j], nums[len(nums)-j-1]), max(nums[j], nums[len(nums)-j-1])
		// using prefix sum: most interesting point, and is the key to reduce complexity
		diff[2] += 2
		diff[a+1]--
		diff[a+b]--
		diff[a+b+1]++
		diff[b+limit+1]++
	}
	cur, res := 0, len(nums)
	for i := 2; i <= 2*limit; i++ {
		cur += diff[i]
		res = min(res, cur)
	}
	return res
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

Calendar Jun 25, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文