Complete the code to initialize the dp array with zeros.
int dp[[1] + 1]; for (int i = 0; i <= W; i++) { dp[i] = 0; }
The dp array size should be W + 1 to cover all weights from 0 to W.
Complete the code to update dp[i] inside the loop for unbounded knapsack.
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]); } } }
We use max to keep the maximum value achievable for weight i.
Fix the error in the loop condition to avoid out-of-bound access.
for (int j = 0; j < [1]; j++) { // loop body }
The inner loop must run over the number of items n, not weight W or arrays dp or val.
Fill both blanks to complete the function signature and return statement.
int unboundedKnapsack(int [1], int [2], int val[], int wt[]) { // function body return dp[W]; }
The function takes number of items n and max weight W as parameters.
Fill all three blanks to complete the main loop and dp update for unbounded knapsack.
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]); } } }
The outer loop runs from 0 to W (max weight), inner loop runs over n items.
