FastPrepAlternating Index Square Sum

Alternating Index Square Sum

Accenture logoAccenture● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given an integer array values, compute an alternating signed sum of squares:

  • Add values[i]^2 when i is even.
  • Subtract values[i]^2 when i is odd.

Indexes are zero-based. Return the resulting 64-bit integer.

Function

alternatingSquareSum(values: int[]) → long

Examples

Example 1

values = [1,2,3,4]return = -10

The result is 1^2 - 2^2 + 3^2 - 4^2 = -10.

Example 2

values = [5]return = 25

The only element is at even index 0, so its square is added.

Example 3

values = [-2,-3,4]return = 11

Squaring 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.

More Accenture problems

See Accenture hiring insights
public long alternatingSquareSum(int[] values) {
    // Write your code here.
}
values[1,2,3,4]
expected-10
Checking account…