Problem 330500 · easy · Phase 03 Linear Management & Searching

Halfway Point of the Load

prefix sums · thresholds

Crates with positive integer weights are loaded onto a truck in order. Return the index of the crate at which the cumulative weight first reaches at least half of the total weight, i.e. the smallest i with 2 * (weights[0] + ... + weights[i]) >= total. Return -1 for an empty list.

Examples

Input:  weights = [1, 2, 3, 4]
Output: 2
Explanation: total is 10; after crates 0..2 the cumulative weight is 6 >= 5.

Input:  weights = [1, 1, 1, 1]
Output: 1
Explanation: 2 * 2 >= 4 already after index 1.

Input:  weights = []
Output: -1

Constraints

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

Goals

  • Compare a running total against the grand total
  • Handle the empty input explicitly
Starting Python…