Alternating Index Square Sum
Problem statement
Given an integer array values, compute an alternating signed sum of squares:
- Add
values[i]^2wheniis even. - Subtract
values[i]^2wheniis odd.
Indexes are zero-based. Return the resulting 64-bit integer.
Function
alternatingSquareSum(values: int[]) → longExamples
Example 1
values = [1,2,3,4]return = -10The result is 1^2 - 2^2 + 3^2 - 4^2 = -10.
Example 2
values = [5]return = 25The only element is at even index 0, so its square is added.
Example 3
values = [-2,-3,4]return = 11Squaring removes the signs, so the result is 4 - 9 + 16 = 11.
Constraints
1 <= values.length <= 10^5.-10^6 <= values[i] <= 10^6.- The final result fits in a signed 64-bit integer.