Problem 387146 · easy · Level 03 Linear Management & Searching

Front-Heavy Cuts

prefix sums · counting · splits

Seed packets are lined up with weights weights. A cut after position i (with 0 <= i < n - 1) splits the line into a front part weights[0..i] and a back part weights[i+1..n-1], both non-empty. Count the cuts where the front part weighs at least as much as the back part.

Examples

Input:  weights = [6, -2, 3, 5]
Output: 2
Explanation: cut after index 0: 6 >= 6 (a tie counts); after index 1: 4 >= 8 fails; after index 2: 7 >= 5.

Input:  weights = [1]
Output: 0
Explanation: no cut leaves both parts non-empty.

Constraints

  • 1 <= len(weights) <= 10**5
  • -10**5 <= weights[i] <= 10**5
  • Target complexity: O(n) time, O(1) extra space.

Goals

  • Evaluate every split point with a running left total
  • Derive the right total from the grand total
Starting Python…