Balanced Alternating Sums After One Removal
Arrays & HashingAuto-graded · 23 test casesLast updated
Count positions whose removal makes the sum at even positions equal the sum at odd positions, testing prefix/suffix sums and the parity shift after a deletion.
You are given a list of integers values. Positions are numbered from 0.
Suppose you remove exactly one element, at position p. Every element that was to the right of p moves one place to the left, so the positions in the shortened list are renumbered from 0.
Call the shortened list balanced if the sum of its elements at even positions equals the sum of its elements at odd positions. An empty list has both sums equal to 0, so it is balanced.
Return the number of positions p (each considered independently, on the original list) for which removing the element at p produces a balanced list.
Each removal is evaluated separately on the original list. Different positions count separately even if they hold equal values.
Constraints
0 <= len(values) <= 10^5- Values are integers and may be zero or negative; the sums fit in a standard 64-bit integer.
- An empty input returns
0. - A single-element list returns
1, since removing that element leaves an empty, balanced list.
Examples
Example 1
Input: count_balanced_removals([2, 1, 6, 4])
Output: 1
Example 2
Input: count_balanced_removals([1, 1, 1])
Output: 3
Example 3
Input: count_balanced_removals([1, 2, 3])
Output: 0
Hints
Hint 1
Elements before the removed position keep their positions. Elements after it shift left by one, so their even/odd roles swap.
Hint 2
After removing position p, the even-position sum is (even-position sum of the left part) + (odd-position sum of the original right part), and similarly for the odd-position sum.
Hint 3
Start with the total even and odd sums, then sweep left to right. Subtract the current element from its parity total before testing, and add it to the left accumulators only after testing.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
When the element at position i is removed:
- Elements to its left keep their positions, so their parity is unchanged.
- Elements to its right shift left by one, so even positions become odd and odd positions become even.
So after removal:
- new even sum = (even-position sum of the left part) + (odd-position sum of the right part)
- new odd sum = (odd-position sum of the left part) + (even-position sum of the right part)
Here "left part" means original positions 0..i-1 and "right part" means i+1..n-1, both using original positions.
We compute the total even and odd sums once. Then we sweep left to right, keeping the left accumulators. At each index i:
- Subtract
values[i]from the total of its parity. The totals now describe the right part strictly afteri. - Test
left_even + right_odd == left_odd + right_even. If true, count it. - Add
values[i]to the left accumulator of its parity.
Why it is correct
At the moment of the test, the left accumulators contain exactly positions 0..i-1 and the right totals contain exactly positions i+1..n-1. By the parity-swap argument, the condition is exactly the balance condition for the list with i removed. Every index is tested once, so the count is exact. A single-element list leaves an empty list (0 == 0), and an empty input never enters the loop, so it returns 0.
Complexity
- Time: O(n)
- Space: O(1) extra (the slices used for the initial sums are temporary; an index loop avoids them)
Reference solution
from typing import List
def count_balanced_removals(values: List[int]) -> int:
right_even = sum(values[::2])
right_odd = sum(values[1::2])
left_even = 0
left_odd = 0
count = 0
for i, v in enumerate(values):
# Remove the current element from the right-side totals.
if i % 2 == 0:
right_even -= v
else:
right_odd -= v
# Right part's parities swap after the removal.
if left_even + right_odd == left_odd + right_even:
count += 1
# Move the current element into the left-side totals.
if i % 2 == 0:
left_even += v
else:
left_odd += v
return count