Ctrl K

Three Parts in Rising Order

Medium
You get an array of non-negative integers values. It is cut into three non-empty contiguous pieces: left, middle and right. A cut is **good** if the sum of the left piece is at most the sum of the middle one, and the sum of the middle piece is at most the sum of the right one. Return the number of good cuts. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
values=[2, 2, 3, 1, 4] → 3

Good cuts are [2] [2] [3, 1, 4] (2 ≤ 2 ≤ 8), [2] [2, 3] [1, 4] (2 ≤ 5 ≤ 5) and [2, 2] [3, 1] [4] (4 ≤ 4 ≤ 4). For instance, [2] [2, 3, 1] [4] fails because 6 > 4. So the answer is 3.

Example 2
values=[5, 4, 3] → 0

The only split is [5] [4] [3], but 5 > 4, so the answer is 0.

Example 3
values=[0, 0, 0, 0] → 3
Constraints
  • 3 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsClimbing Gym Points MediumFlowerbed Beauty on a Budget HardPair in a Sorted List MediumShortest Stretch to the Goal MediumStudios for Recording Sessions Medium
loading Python…
Input
values=[2, 2, 3, 1, 4]
Expected
3

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