Ctrl K

Removals That Balance Parity Sums

Medium
You get an array of positive integers values. Exactly one element is removed from it; the elements to its right shift one place to the left, so their indices drop by 1. Call an array **balanced** if the sum of its elements at even indices equals the sum of its elements at odd indices (indices are 0-based). Return the number of indices whose removal leaves the array balanced.
Example 1
values=[3, 5, 2, 4] → 1

Remove index 0: [5, 2, 4], even 5 + 4 = 9, odd 2. Index 1: [3, 2, 4], 7 and 2. Index 2: [3, 5, 4], 7 and 5. Index 3: [3, 5, 2], 5 and 5, equal. Only one index works.

Example 2
values=[7, 7, 7, 7, 7] → 5

Removing any element leaves four 7s, and both sums are 14, so all 5 indices work.

Example 3
values=[2, 9] → 0
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumProduct of All the Others MediumStudios for Recording Sessions MediumLongest Run with a Given Sum MediumBatch of Segment Adjustments Medium
loading Python…
Input
values=[3, 5, 2, 4]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.