Ways to Make a Fair Array
Real-world version: Which Single Removal Balances the HalvesThe 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.
def ways_to_make_fair(readings: list[int]) -> int:public int waysToMakeFair(int[] readings)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's