All problems
1084MediumArrayPrefix Sum

Which Single Removal Balances the Halves

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1664Ways to Make a Fair Array

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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