All problems
1039MediumArrayPrefix Sum

Cuts Where the Front Holds Its Own

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2270Number of Ways to Split 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. 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 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_split_array(readings: list[int]) -> int:
Java
public int waysToSplitArray(int[] readings)
September 7
Apply