Maximize an alternating squared score by rearranging an integer array. Covers signed values, fresh examples, an optimality proof, numeric bounds, and C++ code.
### Problem
You may rearrange all elements of an integer array `nums` in any order. For a rearranged array `arr`, define its alternating squared score as:
```text
arr[0]^2 - arr[1]^2 + arr[2]^2 - arr[3]^2 + ...
```
Return the greatest score attainable by a rearrangement. Indices are zero-based, the first term is added, and every original element must be used once with its original value.
```cpp
class Solution {
public:
long long maxAlternatingSum(vector<int>& nums);
};
```
### Examples
**Example 1:**
```text
Input: nums = [-4,0,2]
Output: 20
Explanation: The arrangement [-4,0,2] scores 16-0+4 = 20. No rearrangement has a greater score.
```
**Example 2:**
```text
Input: nums = [-3,3]
Output: 0
Explanation: Either order scores 9-9 = 0. Moving a value does not change its sign before it is squared.
```
### Constraints
- `1 <= nums.length <= 100000`.
- `-40000 <= nums[i] <= 40000`.
### Clarifying Questions
- Must all positions be used? Yes; this is a rearrangement, not a subsequence selection.
- May a moved value have its sign changed? No; it retains its original value.
- Do repeated values represent separate elements? Yes.
- Must an arrangement be returned? No; return only its maximum score using the original `long long` API.
### What a Strong Answer Covers
- The allowed rearrangement and the exact alternating squared-score definition.
- An attainable score with a proof that no larger score exists.
- Negative values, zeros, repeated values, and both odd and even lengths.
- Numeric safety and resource bounds for the full constraints.
### Follow-up Questions
- What extra output would be needed to return a maximizing rearrangement?
- How would requiring the original order change the permitted operations?
```hint Compare which terms receive each sign
Only squared magnitudes affect the score. There are fixed numbers of positive and negative positions. Consider exchanging two elements that occupy opposite-sign positions, and determine when that exchange improves the score.
```
Overview: Maximize an alternating squared score by rearranging an integer array. Covers signed values, fresh examples, an optimality proof, numeric bounds, and C++ code.
You may rearrange all elements of an integer array nums in any order. For a rearranged array arr, define its alternating squared score as:
arr[0]^2 - arr[1]^2 + arr[2]^2 - arr[3]^2 + ...
Return the greatest score attainable by a rearrangement. Indices are zero-based, the first term is added, and every original element must be used once with its original value.
class Solution {
public:
long long maxAlternatingSum(vector<int>& nums);
};
Examples
Example 1:
Input: nums = [-4,0,2]
Output: 20
Explanation: The arrangement [-4,0,2] scores 16-0+4 = 20. No rearrangement has a greater score.
Example 2:
Input: nums = [-3,3]
Output: 0
Explanation: Either order scores 9-9 = 0. Moving a value does not change its sign before it is squared.
Constraints
1 <= nums.length <= 100000
.
-40000 <= nums[i] <= 40000
.
Clarifying Questions Guidance
Must all positions be used? Yes; this is a rearrangement, not a subsequence selection.
May a moved value have its sign changed? No; it retains its original value.
Do repeated values represent separate elements? Yes.
Must an arrangement be returned? No; return only its maximum score using the original
long long
API.
What a Strong Answer Covers Guidance
The allowed rearrangement and the exact alternating squared-score definition.
An attainable score with a proof that no larger score exists.
Negative values, zeros, repeated values, and both odd and even lengths.
Numeric safety and resource bounds for the full constraints.
Follow-up Questions Guidance
What extra output would be needed to return a maximizing rearrangement?
How would requiring the original order change the permitted operations?