Ctrl K

Front-Heavy Splits

Medium
You are given an integer array values. Cut it into two non-empty parts, a front and a back. A cut is good if the sum of the front is at least the sum of the back. Return the number of good cuts.
Example 1
values=[3, -1, 2, -4] → 3

The whole array sums to 0. After the first element it is 3 vs -3, after the second 2 vs -2, after the third 4 vs -4: all three cuts work.

Example 2
values=[1, 5] → 0

The only cut gives 1 vs 5, so the left part is lighter.

Example 3
values=[-2, -2, -2] → 1

Cutting after the first element gives -2 ≥ -4, which works. Cutting after the second gives -4 < -2, which does not.

Constraints
  • 2 ≤ values.length ≤ 10⁵
  • -10⁵ ≤ values[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumProduct of All the Others MediumStudios for Recording Sessions MediumLongest Run with a Given Sum MediumBatch of Segment Adjustments Medium
loading Python…
Input
values=[3, -1, 2, -4]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.