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] = target for some target value. 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.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3,4], limit = 4
›
Output:
0
💡 Note:
Pairs are (1,4) with sum 5 and (2,3) with sum 5. Both pairs already sum to the same target value 5, so 0 moves are needed.
Example 2 — No Changes Needed
$
Input:
nums = [1,4,2,3], limit = 4
›
Output:
1
💡 Note:
Pairs are (1,3) with sum 4 and (4,2) with sum 6. For target sum 4, the first pair needs 0 moves and the second pair needs 1 move (change 4→2 or 2→0, but since values must be ≥1, change 4→2). Total: 1 move.
Example 3 — All Changes Required
$
Input:
nums = [1,1,1,1], limit = 2
›
Output:
0
💡 Note:
Pairs are (1,1) with sum 2 and (1,1) with sum 2. Both pairs already sum to 2, so 0 moves are needed.
Constraints
- n == nums.length
- 2 ≤ n ≤ 105
- 1 ≤ limit ≤ 105
- 1 ≤ nums[i] ≤ limit
- n is even
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code