Show problems as
Classic problem #1664MediumArrayPrefix Sum

Ways to Make a Fair Array

Real-world version: Which Single Removal Balances the Halves
Tracked in this browser only
Read Ways to Make a Fair Array on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A strip of readings reads readings. Removing exactly one reading closes the gap behind it, so everything after it shifts one place along.

A strip is balanced when the readings at even positions add up to the same as the readings at odd positions, counting positions from zero.

Return how many single removals leave the strip balanced.

Examples

Example 1

Input
readings = [1]
Output
1

Removing the only reading leaves nothing at all, so both totals are nothing and the strip is balanced.

Example 2

Input
readings = [5, 5]
Output
0

Either removal leaves one reading at an even position, so the even total is five against nothing.

Example 3

Input
readings = [1, 2, 1, 2]
Output
1

Removing the first reading leaves 2, 1, 2 with an even total of four against an odd total of one, and so on for the others; only one of the four removals balances.

Constraints

  • 1 <= readings.length <= 10^5
  • 1 <= readings[i] <= 10^4

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def ways_to_make_fair(readings: list[int]) -> int:
Java
public int waysToMakeFair(int[] readings)