Ctrl K

Target Picks in Every Basket

Hard
You are given an array of positive integers values of length n and an integer goal. A **basket** is any subsequence of values: you choose elements by position and keep their order. There are 2ⁿ baskets, the empty one and the whole array included. Equal values at different positions count as different elements. The **weight** of a basket is the number of its own subsequences whose sum is exactly goal. Return the total weight of all baskets. The answer can be huge, so return it modulo 10⁹ + 7.
Example 1
values=[2, 1, 1], goal=2 → 6

Two picks sum to 2: [2] and [1, 1]. The pick [2] lies in 4 baskets (each 1 may or may not be added), and [1, 1] lies in 2. In total, 4 + 2 = 6.

Example 2
values=[5, 6], goal=4 → 0

Every element exceeds 4, so no basket can reach a sum of 4.

Example 3
values=[3], goal=3 → 1
Constraints
  • 1 ≤ n ≤ 1000
  • 1 ≤ values[i] ≤ 10⁴
  • 1 ≤ goal ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWater Between Walls HardBiggest Solid Panel Block HardProfit From Two Trades HardEvery Way to Space the Words HardCrossing the Cave Hard
loading Python…
Input
values=[2, 1, 1], goal=2
Expected
6

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