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.This problem asks us to find the minimum number of moves needed to make an array "complementary," meaning that the sum of each pair of numbers symmetrically positioned around the array's center is the same. We can achieve this by changing the value of any number within a given limit.
The most efficient approach uses a difference array technique. This approach avoids explicitly calculating all possible sums and their corresponding moves. Instead, it focuses on the changes in the number of moves required as we consider different potential sums.
Initialization: Create a difference array d
of size 2 * limit + 2
. This array will track the changes in the number of moves needed for different sums. The index represents a potential sum, and the value at that index reflects the change in the number of moves required for that sum.
Iterate through Pairs: Iterate through the pairs of numbers symmetrically positioned around the middle of the nums
array. For each pair (nums[i], nums[n-1-i])
:
Find the minimum (x
) and maximum (y
) values in the pair.
Based on the possible sums and the number of changes needed, update the difference array d
as follows:
d[2] += 2
: Initially, assume two changes are needed for any sum.d[x + 1] -= 2
: Subtract 2 because if the sum is greater than or equal to x + 1
, one fewer change is needed.d[x + 1] += 1
: Add 1 to account for the case where one change is enough.d[x + y] -= 1
: Subtract 1 as no changes are needed if the sum is equal to x + y
.d[x + y + 1] += 1
: Add 1 since one change becomes needed if the sum is x + y + 1
.d[y + limit + 1] -= 1
: Subtract 1 as the sum becoming greater than y + limit
requires additional changes.d[y + limit + 1] += 2
: Add 2 changes if sum exceeds the limit, accounting for two replacements.Calculate Minimum Moves: Iterate through the d
array (starting from index 2) and calculate the prefix sums. The minimum of these prefix sums represents the minimum number of moves needed to make the array complementary. This minimum value captures the overall minimum number of changes required across all possible target sums.
nums
(O(n)) and the difference array (O(limit)). In most cases, n
will be significantly smaller than limit
.d
.The code examples in the original response provide implementations of this difference array approach in various programming languages. They accurately reflect the algorithm described above, efficiently calculating the minimum number of moves. Refer to those code snippets for detailed implementations in Python, Java, C++, Go, and TypeScript. Each code sample demonstrates how the difference array is constructed and used to efficiently solve the problem.