← Quiz: 0/1 Knapsack and Subset Sum: all 10 questions
Quiz: 0/1 Knapsack and Subset Sum · Question 3 of 10
What does this print? (code)
What does this print?
wt, val, W = [2, 3, 4], [3, 4, 6], 5
dp = [0] * (W + 1)
for w, v in zip(wt, val):
for c in range(W, w - 1, -1):
dp[c] = max(dp[c], v + dp[c - w])
print(dp[W])
- A.6
- B.7
- C.9
- D.10
Answer & explanation
Correct answer: 7
Capacity 5 fits weights 2 + 3 (value 7). Weights 2 + 4 = 6 is too heavy, and 4 alone gives only 6.
Can you get the rest right without peeking?
Take the timed Quiz: 0/1 Knapsack and Subset Sum quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →