Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with zeros.
DSA C++
int dp[[1] + 1] = {0};
Attempts:
3 left
💡 Hint
Common Mistakes
Using coins.size() instead of amount for dp array size.
Forgetting to add 1 to amount for array size.
✗ Incorrect
We need an array of size amount + 1 to store ways to make each amount from 0 to amount.
2fill in blank
mediumComplete the code to set the base case for dp array.
DSA C++
dp[[1]] = 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[1] = 1 instead of dp[0].
Setting dp[amount] = 1 incorrectly.
✗ Incorrect
There is exactly one way to make amount 0: by choosing no coins.
3fill in blank
hardFix the error in the loop condition to iterate over amounts correctly.
DSA C++
for (int j = coins[i]; j [1] amount; j++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using < causes last amount to be skipped.
Using > or >= causes incorrect loop behavior.
✗ Incorrect
We need to include amount itself, so the loop should run while j <= amount.
4fill in blank
hardFill both blanks to update dp array with ways including current coin.
DSA C++
dp[j] [1]= dp[j - coins[i]];Attempts:
3 left
💡 Hint
Common Mistakes
Using -= or *= instead of += causes wrong results.
Using / operator causes runtime error.
✗ Incorrect
We add ways to make amount j - coins[i] to dp[j] to include current coin.
5fill in blank
hardFill all three blanks to complete the coin change function.
DSA C++
int change(int amount, vector<int>& coins) {
vector<int> dp(amount + 1, 0);
dp[[1]] = 1;
for (int i = 0; i [2] coins.size(); i++) {
for (int j = coins[i]; j [3] amount; j++) {
dp[j] += dp[j - coins[i]];
}
}
return dp[amount];
}Attempts:
3 left
💡 Hint
Common Mistakes
Wrong base case index.
Using <= in outer loop causing out-of-bounds.
Using < in inner loop causing last amount to be skipped.
✗ Incorrect
dp[0] = 1 as base case; outer loop runs while i < coins.size(); inner loop runs while j <= amount.
