From 30af6a8b6b4e372f8910ac25962e262fd3404fdc Mon Sep 17 00:00:00 2001 From: namanhere23 Date: Thu, 30 Oct 2025 20:18:09 +0530 Subject: [PATCH] Add Minimum Number of Coins --- DSA/MinimumNumberOfCoins.cpp | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 DSA/MinimumNumberOfCoins.cpp diff --git a/DSA/MinimumNumberOfCoins.cpp b/DSA/MinimumNumberOfCoins.cpp new file mode 100644 index 0000000..4c2727d --- /dev/null +++ b/DSA/MinimumNumberOfCoins.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +int minCoins(vector& coins, int amount) { + const int INF = 1e9; + vector dp(amount + 1, INF); + dp[0] = 0; + + for (int coin : coins) { + for (int x = coin; x <= amount; x++) { + dp[x] = min(dp[x], dp[x - coin] + 1); + } + } + + return (dp[amount] == INF) ? -1 : dp[amount]; +} + +int main() { + int n, amount; + cout << "Enter number of coin types: "; + cin >> n; + + vector coins(n); + cout << "Enter coin denominations: "; + for (int i = 0; i < n; i++) + cin >> coins[i]; + + cout << "Enter the amount: "; + cin >> amount; + + int result = minCoins(coins, amount); + + if (result == -1) + cout << "It is not possible to make the amount with given coins"<