Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the ways array with 0.
DSA C
int ways[amount + 1] = {0}; ways[0] = [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting ways[0] to 0 will cause the algorithm to fail to count any combinations.
Using negative or amount value instead of 1.
✗ Incorrect
ways[0] = 1 means there is exactly one way to make amount 0: by choosing no coins.
2fill in blank
mediumComplete the code to update the ways array inside the nested loop.
DSA C
for (int i = 0; i < n; i++) { for (int j = coins[i]; j <= amount; j++) { ways[j] += [1]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding coins[i] directly instead of ways[j - coins[i]].
Using ways[j] on the right side causes incorrect counting.
✗ Incorrect
ways[j] is updated by adding ways[j - coins[i]] which represents the number of ways to make amount j - coins[i].
3fill in blank
hardFix the error in the loop condition to avoid out-of-bounds access.
DSA C
for (int j = coins[i]; j [1] amount; j++) { ways[j] += ways[j - coins[i]]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < causes the last amount to be skipped.
Using > or >= causes the loop to not run or run incorrectly.
✗ Incorrect
The loop must run while j is less than or equal to amount to cover all amounts up to the target.
4fill in blank
hardFill both blanks to complete the function signature and return statement.
DSA C
int [1](int coins[], int n, int amount) { int ways[amount + 1] = {0}; ways[0] = 1; for (int i = 0; i < n; i++) { for (int j = coins[i]; j <= amount; j++) { ways[j] += ways[j - coins[i]]; } } return [2]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning amount or coins instead of ways[amount].
Using a wrong function name that does not match the problem.
✗ Incorrect
The function is named countWays and returns ways[amount], the total number of ways to make the amount.
5fill in blank
hardFill all three blanks to complete the main function that calls countWays and prints the result.
DSA C
#include <stdio.h> int countWays(int coins[], int n, int amount); int main() { int coins[] = {1, 2, 5}; int n = sizeof(coins) / sizeof(coins[0]); int amount = 5; int result = [1](coins, n, amount); printf("Total ways to make %d: %d\n", [2], [3]); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in printf.
Calling a wrong function or passing wrong arguments.
✗ Incorrect
Call countWays with coins, n, amount; print amount and result to show total ways.