0
0
DSA Cprogramming~20 mins

Coin Change Minimum Coins in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coin Change Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of minimum coins for amount 11
What is the output of the following C code that calculates the minimum number of coins needed to make amount 11 using coins {1, 2, 5}?
DSA C
#include <stdio.h>
#include <limits.h>

int minCoins(int coins[], int n, int amount) {
    int dp[amount+1];
    dp[0] = 0;
    for (int i = 1; i <= amount; i++) {
        dp[i] = INT_MAX;
        for (int j = 0; j < n; j++) {
            if (coins[j] <= i && dp[i - coins[j]] != INT_MAX) {
                int sub_res = dp[i - coins[j]] + 1;
                if (sub_res < dp[i])
                    dp[i] = sub_res;
            }
        }
    }
    return dp[amount];
}

int main() {
    int coins[] = {1, 2, 5};
    int amount = 11;
    int n = sizeof(coins)/sizeof(coins[0]);
    int result = minCoins(coins, n, amount);
    printf("%d\n", result);
    return 0;
}
A3
B4
C5
D2
Attempts:
2 left
💡 Hint
Think about the fewest coins that sum to 11 using 1, 2, and 5.
🧠 Conceptual
intermediate
1:30remaining
Understanding the DP array in Coin Change
In the coin change minimum coins problem, what does the dp array represent after the algorithm finishes?
Adp[i] stores the coin denomination used last to make amount i
Bdp[i] stores the maximum number of coins needed to make amount i
Cdp[i] stores the total number of ways to make amount i
Ddp[i] stores the minimum number of coins needed to make amount i
Attempts:
2 left
💡 Hint
Think about what the algorithm tries to minimize for each amount.
🔧 Debug
advanced
2:30remaining
Identify the error causing wrong output
The following code tries to find the minimum coins for amount 7 using coins {2, 3, 6}, but it outputs 2147483647. What is the cause?
DSA C
#include <stdio.h>
#include <limits.h>

int minCoins(int coins[], int n, int amount) {
    int dp[amount+1];
    dp[0] = 0;
    for (int i = 1; i <= amount; i++) {
        dp[i] = INT_MAX;
        for (int j = 0; j < n; j++) {
            if (coins[j] < i && dp[i - coins[j]] != INT_MAX) {
                int sub_res = dp[i - coins[j]] + 1;
                if (sub_res < dp[i])
                    dp[i] = sub_res;
            }
        }
    }
    return dp[amount];
}

int main() {
    int coins[] = {2, 3, 6};
    int amount = 7;
    int n = sizeof(coins)/sizeof(coins[0]);
    int result = minCoins(coins, n, amount);
    printf("%d\n", result);
    return 0;
}
AThe coins array is missing the coin 1
BThe condition should be coins[j] <= i instead of coins[j] < i
CThe return statement should return dp[amount-1]
Ddp array is not initialized for dp[0]
Attempts:
2 left
💡 Hint
Check the condition that decides if a coin can be used for amount i.
🚀 Application
advanced
1:00remaining
Minimum coins for amount 0
What is the minimum number of coins needed to make amount 0 using any set of coins?
A-1
B1
C0
DUndefined
Attempts:
2 left
💡 Hint
Think about how many coins you need to make zero amount.
Predict Output
expert
2:00remaining
Output of minimum coins with no solution
What is the output of the following code when trying to make amount 7 with coins {5, 10}?
DSA C
#include <stdio.h>
#include <limits.h>

int minCoins(int coins[], int n, int amount) {
    int dp[amount+1];
    dp[0] = 0;
    for (int i = 1; i <= amount; i++) {
        dp[i] = INT_MAX;
        for (int j = 0; j < n; j++) {
            if (coins[j] <= i && dp[i - coins[j]] != INT_MAX) {
                int sub_res = dp[i - coins[j]] + 1;
                if (sub_res < dp[i])
                    dp[i] = sub_res;
            }
        }
    }
    if (dp[amount] == INT_MAX) return -1;
    return dp[amount];
}

int main() {
    int coins[] = {5, 10};
    int amount = 7;
    int n = sizeof(coins)/sizeof(coins[0]);
    int result = minCoins(coins, n, amount);
    printf("%d\n", result);
    return 0;
}
A-1
B2
C1
D0
Attempts:
2 left
💡 Hint
Check if amount 7 can be formed by coins 5 and 10.