Show problems as
Classic problem #2270MediumArrayPrefix Sum

Number of Ways to Split Array

Real-world version: Cuts Where the Front Holds Its Own
Tracked in this browser only
Read Number of Ways to Split 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. A cut falls between two neighbouring readings, so there is one fewer cut than there are readings, and every cut leaves a front piece and a back piece, neither of them empty.

A cut is sound when the front piece's total is at least the back piece's.

Return how many cuts are sound.

Examples

Example 1

Input
readings = [5, -5, 5, -5]
Output
3

The whole strip totals nothing. After the first reading the front holds five and the back minus five, after the second both hold nothing, and after the third the front holds five again, so all three cuts are sound.

Example 2

Input
readings = [1, 6]
Output
0

The only cut leaves a front of one against a back of six, so it is not sound.

Example 3

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

The first cut leaves three against nine and fails. The second leaves six against six and passes, and the third leaves nine against three and passes too.

Constraints

  • 2 <= readings.length <= 10^5
  • -10^5 <= readings[i] <= 10^5

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_split_array(readings: list[int]) -> int:
Java
public int waysToSplitArray(int[] readings)