Challenge - 5 Problems
Knapsack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of 0 1 Knapsack DP Table
What is the final maximum value stored in the DP table for the given weights and values?
DSA C++
int knapsack(int W, int wt[], int val[], int n) { int dp[n+1][W+1]; for (int i = 0; i <= n; i++) { for (int w = 0; w <= W; w++) { if (i == 0 || w == 0) dp[i][w] = 0; else if (wt[i-1] <= w) dp[i][w] = std::max(val[i-1] + dp[i-1][w - wt[i-1]], dp[i-1][w]); else dp[i][w] = dp[i-1][w]; } } return dp[n][W]; } int main() { int val[] = {60, 100, 120}; int wt[] = {10, 20, 30}; int W = 50; int n = sizeof(val)/sizeof(val[0]); std::cout << knapsack(W, wt, val, n); return 0; }
Attempts:
2 left
💡 Hint
Think about which items fit into the knapsack without exceeding the weight limit and maximize value.
✗ Incorrect
The knapsack can hold items with weights 20 and 30 for a total value of 100 + 120 = 220, which is the maximum possible value without exceeding weight 50.
🧠 Conceptual
intermediateUnderstanding the 0 1 Knapsack Problem Constraints
Which statement correctly describes the 0 1 Knapsack problem?
Attempts:
2 left
💡 Hint
Remember the meaning of '0 1' in the problem name.
✗ Incorrect
In the 0 1 Knapsack problem, each item can be chosen either once or not at all, no fractions or repetitions allowed.
🔧 Debug
advancedIdentify the Bug in the Knapsack Recursive Code
What error will this code produce when called with knapsack(50, wt, val, 3)?
DSA C++
int knapsack(int W, int wt[], int val[], int n) { if (n == 0 || W == 0) return 0; if (wt[n-1] > W) return knapsack(W, wt, val, n-1); else return std::max(val[n-1] + knapsack(W - wt[n-1], wt, val, n-1), knapsack(W, wt, val, n-1)); } int main() { int val[] = {60, 100, 120}; int wt[] = {10, 20, 30}; std::cout << knapsack(50, wt, val, 3); return 0; }
Attempts:
2 left
💡 Hint
Check the base case and recursive calls carefully.
✗ Incorrect
The recursive code correctly implements the 0 1 Knapsack logic and will output 220 for the given inputs.
❓ Predict Output
advancedOutput of Memoized Knapsack Function
What is the output of the following code snippet?
DSA C++
#include <iostream> #include <cstring> int dp[4][51]; int knapsack(int W, int wt[], int val[], int n) { if (n == 0 || W == 0) return 0; if (dp[n][W] != -1) return dp[n][W]; if (wt[n-1] <= W) { dp[n][W] = std::max(val[n-1] + knapsack(W - wt[n-1], wt, val, n-1), knapsack(W, wt, val, n-1)); } else { dp[n][W] = knapsack(W, wt, val, n-1); } return dp[n][W]; } int main() { int val[] = {60, 100, 120}; int wt[] = {10, 20, 30}; memset(dp, -1, sizeof(dp)); std::cout << knapsack(50, wt, val, 3); return 0; }
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
✗ Incorrect
Memoized recursion returns the maximum value 220 for the given weights and values within capacity 50.
🚀 Application
expertMaximum Number of Items in 0 1 Knapsack
Given the weights and values below and knapsack capacity 50, what is the maximum number of items that can be included in the knapsack while achieving the maximum total value?
DSA C++
int val[] = {60, 100, 120}; int wt[] = {10, 20, 30}; int W = 50;
Attempts:
2 left
💡 Hint
Check which combination yields max value and count items used.
✗ Incorrect
The max value 220 is achieved by taking items with weights 20 and 30, so 2 items are included.
