Class Solution
-
- All Implemented Interfaces:
public final class Solution3727 - Maximum Alternating Sum of Squares.
Medium
You are given an integer array
nums. You may rearrange the elements in any order.The alternating score of an array
arris defined as:<code>score = arr0<sup>2</sup> - arr1<sup>2</sup> + arr2<sup>2</sup> - arr3<sup>2</sup> + ...</code>
Return an integer denoting the maximum possible alternating score of
numsafter rearranging its elements.Example 1:
Input: nums = 1,2,3
Output: 12
Explanation:
A possible rearrangement for
numsis[2,1,3], which gives the maximum alternating score among all possible rearrangements.The alternating score is calculated as:
<code>score = 2<sup>2</sup> - 1<sup>2</sup> + 3<sup>2</sup> = 4 - 1 + 9 = 12</code>
Example 2:
Input: nums = 1,-1,2,-2,3,-3
Output: 16
Explanation:
A possible rearrangement for
numsis[-3,-1,-2,1,3,2], which gives the maximum alternating score among all possible rearrangements.The alternating score is calculated as:
<code>score = (-3)<sup>2</sup> - (-1)<sup>2</sup> + (-2)<sup>2</sup> - (1)<sup>2</sup> + (3)<sup>2</sup> - (2)<sup>2</sup> = 9 - 1 + 4 - 1 + 9 - 4 = 16</code>
Constraints:
<code>1 <= nums.length <= 10<sup>5</sup></code>
<code>-4 * 10<sup>4</sup><= numsi<= 4 * 10<sup>4</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final LongmaxAlternatingSum(IntArray nums)-
-
Method Detail
-
maxAlternatingSum
final Long maxAlternatingSum(IntArray nums)
-
-
-
-