Skip to the code
+50 XP

#44 Three-Way Revenue Split

hard

A partnership report splits a year of monthly revenue revenue (all values 0 or more) into three non-empty back-to-back periods: an early part, a middle part, and a late part.

A split is fair if sum(early) <= sum(middle) <= sum(late). Write fairSplits(revenue) that returns how many fair splits exist. Two splits are different if either cut is in a different place.

Example 1

Input: revenue = [1,1,1]
Output: 1

The only split is [1] [1] [1], and 1 <= 1 <= 1.

Example 2

Input: revenue = [1,2,3,4]
Output: 2

[1] [2] [3, 4] and [1, 2] [3] [4] are fair; [1] [2, 3] [4] is not, since 5 > 4.

Constraints

  • 3 <= revenue.length <= 10^5
  • 0 <= revenue[i] <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).