Problem · Array

Difference Between Sums of Positions

Learn this problem
EasyDatabricks logoDatabricksINTERNOA

Problem statement

Using 0-based indices, sum the values at even positions that lie in [-100, 100], inclusive. Separately sum the qualifying values at odd positions.

Return even-position sum - odd-position sum. Values outside the allowed range do not contribute.

Function

diffBetweenSumsOfPositions(numbers: int[]) → int

Examples

Example 1

numbers = [101, 3, 4, 359, 2, 5]return = -2

The qualifying even-position values are 4 and 2; the qualifying odd-position values are 3 and 5. The result is 6 - 8 = -2.

Example 2

numbers = [-2, 234, 100, 99, 540, -1]return = 0

The even-position sum is -2 + 100 = 98 and the odd-position sum is 99 - 1 = 98.

Example 3

numbers = [-9]return = -9

The only value is at even index 0 and is inside the allowed range.

Constraints

  • 1 ≤ numbers.length ≤ 10^3
  • -10^3 ≤ numbers[i] ≤ 10^3

More Databricks problems

drafts saved locally
public int diffBetweenSumsOfPositions(int[] numbers) {
  // write your code here
}
numbers[101, 3, 4, 359, 2, 5]
expected-2
checking account