0
0
DSA Cprogramming~10 mins

Unbounded Knapsack Problem in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the dp array with zeros.

DSA C
int dp[[1] + 1];
for (int i = 0; i <= W; i++) {
    dp[i] = 0;
}
Drag options to blanks, or click blank then click option'
AW
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp array size as W instead of W + 1
Not initializing dp array properly
2fill in blank
medium

Complete the code to update dp[i] inside the loop for unbounded knapsack.

DSA C
for (int i = 0; i <= W; i++) {
    for (int j = 0; j < n; j++) {
        if (wt[j] <= i) {
            dp[i] = [1](dp[i], dp[i - wt[j]] + val[j]);
        }
    }
}
Drag options to blanks, or click blank then click option'
Aabs
Bmin
Cmax
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max
Using sum or abs which are incorrect here
3fill in blank
hard

Fix the error in the loop condition to avoid out-of-bound access.

DSA C
for (int j = 0; j < [1]; j++) {
    // loop body
}
Drag options to blanks, or click blank then click option'
AW
Bn
Cdp
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using W instead of n in inner loop
Using array names as loop limits
4fill in blank
hard

Fill both blanks to complete the function signature and return statement.

DSA C
int unboundedKnapsack(int [1], int [2], int val[], int wt[]) {
    // function body
    return dp[W];
}
Drag options to blanks, or click blank then click option'
AW
Bn
Cweight
Ditems
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping order of n and W
Using unclear parameter names
5fill in blank
hard

Fill all three blanks to complete the main loop and dp update for unbounded knapsack.

DSA C
for (int i = [1]; i <= [2]; i++) {
    for (int j = 0; j < [3]; j++) {
        if (wt[j] <= i) {
            dp[i] = max(dp[i], dp[i - wt[j]] + val[j]);
        }
    }
}
Drag options to blanks, or click blank then click option'
A0
BW
Cn
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting i from 1 instead of 0
Using wrong loop limits for j