0
0
DSA Cprogramming~20 mins

0 1 Knapsack Problem in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Knapsack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of 0 1 Knapsack Recursive Solution
What is the output of the following C code that solves the 0 1 Knapsack problem using recursion?
DSA C
#include <stdio.h>

int max(int a, int b) {
    return (a > b) ? a : b;
}

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 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};
    int W = 50;
    int n = sizeof(val)/sizeof(val[0]);
    printf("%d\n", knapsack(W, wt, val, n));
    return 0;
}
A180
B220
C160
D200
Attempts:
2 left
💡 Hint
Think about which items fit in the knapsack without exceeding the weight limit and maximize value.
Predict Output
intermediate
2:00remaining
Output of 0 1 Knapsack Dynamic Programming Table
What is the value stored in dp[3][50] after running the following dynamic programming code for 0 1 Knapsack?
DSA C
#include <stdio.h>

int max(int a, int b) {
    return (a > b) ? a : b;
}

int knapsackDP(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] = 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]);
    printf("%d\n", knapsackDP(W, wt, val, n));
    return 0;
}
A180
B150
C220
D200
Attempts:
2 left
💡 Hint
The dp table stores maximum values for subproblems; dp[n][W] is the answer.
🧠 Conceptual
advanced
1:00remaining
Time Complexity of 0 1 Knapsack DP Solution
What is the time complexity of the dynamic programming solution for the 0 1 Knapsack problem with n items and capacity W?
AO(n * W)
BO(n + W)
CO(n^2 * W)
DO(2^n)
Attempts:
2 left
💡 Hint
Consider the size of the dp table and how many operations fill it.
🔧 Debug
advanced
2:00remaining
Identify the Bug in 0 1 Knapsack DP Implementation
What error will the following code produce when compiled and run?
DSA C
#include <stdio.h>

int max(int a, int b) {
    return (a > b) ? a : b;
}

int knapsackDP(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] = 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]);
    printf("%d\n", knapsackDP(W, wt, val, n));
    return 0;
}
ACompilation error: array size must be constant
BRuntime error: segmentation fault
CLogical error: wrong output value
DNo error, outputs 220
Attempts:
2 left
💡 Hint
Check how the dp array is declared with variable sizes.
🚀 Application
expert
3:00remaining
Maximum Value and Items Selected in 0 1 Knapsack
Given the following code, what is the printed output showing the maximum value and the items selected (by their indices) for the knapsack problem?
DSA C
#include <stdio.h>

int max(int a, int b) {
    return (a > b) ? a : b;
}

void 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] = max(val[i-1] + dp[i-1][w - wt[i-1]], dp[i-1][w]);
            else
                dp[i][w] = dp[i-1][w];
        }
    }
    int res = dp[n][W];
    printf("Maximum value: %d\n", res);

    int w = W;
    printf("Items selected: ");
    for (int i = n; i > 0 && res > 0; i--) {
        if (res != dp[i-1][w]) {
            printf("%d ", i-1);
            res -= val[i-1];
            w -= wt[i-1];
        }
    }
    printf("\n");
}

int main() {
    int val[] = {60, 100, 120};
    int wt[] = {10, 20, 30};
    int W = 50;
    int n = sizeof(val)/sizeof(val[0]);
    knapsack(W, wt, val, n);
    return 0;
}
A
Maximum value: 220
Items selected: 0 2 
B
Maximum value: 220
Items selected: 1 2 
C
Maximum value: 180
Items selected: 1 0 
D
Maximum value: 220
Items selected: 2 1 
Attempts:
2 left
💡 Hint
Trace back from dp[n][W] to find which items were included.