Ctrl K

Bundles Within a Weight Range

Hard
A warehouse holds items whose weights are listed in weights (items of the same weight are indistinguishable). You want to pack a parcel whose weight, the sum of the weights inside, falls between low and high inclusive. Count the **distinct** parcels. Two parcels are the same if they contain the same number of items of every weight. An item of weight x can be used at most as many times as x appears in weights. An empty parcel weighs 0. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
weights=[1, 1, 3], low=2, high=4 → 3

The bundles {1, 1} (weight 2), {3} (weight 3) and {1, 3} (weight 4) fit. {1, 3} is counted once even though either 1 could be picked.

Example 2
weights=[0, 2, 2], low=0, high=2 → 4

The empty bundle, {0}, {2} and {0, 2} fit: the weightless item doubles the number of options.

Example 3
weights=[5, 6], low=1, high=4 → 0
Constraints
  • 1 ≤ len(weights) ≤ 2·10⁴
  • 0 ≤ weights[i] ≤ 2·10⁴
  • The sum of all weights is at most 2·10⁴
  • 0 ≤ low ≤ high ≤ 2·10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Separate Segments With a Given Sum MediumEvery Way to Space the Words HardMoving Window Median HardNarrowest Range Touching Every List HardStretches With Exactly k Kinds Hard
loading Python…
Input
weights=[1, 1, 3], low=2, high=4
Expected
3

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