Equalize All Mirrored Pair Sums. The array nums has even length. One move replaces one entry by an integer in [1,limit]. Make every mirrored pair (i,n-1-i) have the same sum, and return the fewest moves required.
### Problem
The array `nums` has even length. One move replaces one entry by an integer in `[1,limit]`. Make every mirrored pair `(i,n-1-i)` have the same sum, and return the fewest moves required.
```cpp
class Solution {
public:
int minMoves(vector<int>& nums, int limit);
};
```
### Examples
**Example 1:**
```text
nums = [1,2,3,3], limit = 4
Output: 1
Explanation: Change the 3 at index 2 to 2. Both distinct mirrored pairs then sum to 4.
```
**Example 2:**
```text
nums = [2,4,4,2], limit = 4
Output: 2
Explanation: The pair sums are 4 and 8. They cannot match after one change; two changes can make both sums 6.
```
### Constraints
- `n == nums.length`, with even `n` in `[2,10^5]`.
- `1 <= nums[i] <= limit <= 10^5`.
### Clarifying Questions
Each mirrored pair is considered once when computing costs. The target sum is a choice shared by every pair; only the minimum move count is returned.
```hint Inspect the structure
What must be common to every mirrored pair, and what counts as one replacement?
```
### Reasoning checkpoints
- Ensure that every mirrored pair reaches one shared sum using only permitted replacement values.
- Prove that the total move count is minimal, counting a changed occurrence once.
### Optional extensions
How would the method change if replacement values came from an arbitrary sparse set?
Overview: Equalize All Mirrored Pair Sums. The array nums has even length. One move replaces one entry by an integer in [1,limit]. Make every mirrored pair (i,n-1-i) have the same sum, and return the fewest moves required.
The array nums has even length. One move replaces one entry by an integer in [1,limit]. Make every mirrored pair (i,n-1-i) have the same sum, and return the fewest moves required.
class Solution {
public:
int minMoves(vector<int>& nums, int limit);
};
Examples
Example 1:
nums = [1,2,3,3], limit = 4
Output: 1
Explanation: Change the 3 at index 2 to 2. Both distinct mirrored pairs then sum to 4.
Example 2:
nums = [2,4,4,2], limit = 4
Output: 2
Explanation: The pair sums are 4 and 8. They cannot match after one change; two changes can make both sums 6.
Constraints
n == nums.length
, with even
n
in
[2,10^5]
.
1 <= nums[i] <= limit <= 10^5
.
Clarifying Questions Guidance
Each mirrored pair is considered once when computing costs. The target sum is a choice shared by every pair; only the minimum move count is returned.
Reasoning checkpoints
Ensure that every mirrored pair reaches one shared sum using only permitted replacement values.
Prove that the total move count is minimal, counting a changed occurrence once.
Optional extensions
How would the method change if replacement values came from an arbitrary sparse set?