From 8afb85cbb096b3135a7e7bca84772d9ea4a992ff Mon Sep 17 00:00:00 2001 From: amitagoel <73668666+amitagoel@users.noreply.github.com> Date: Mon, 31 Oct 2022 22:26:09 +0530 Subject: [PATCH] Create CoinChangeProblem.cpp --- CoinChangeProblem.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 CoinChangeProblem.cpp diff --git a/CoinChangeProblem.cpp b/CoinChangeProblem.cpp new file mode 100644 index 0000000..ecc24bf --- /dev/null +++ b/CoinChangeProblem.cpp @@ -0,0 +1,46 @@ +// Recursive C++ program for +// coin change problem. +#include +using namespace std; + +// Returns the count of ways we can +// sum coins[0...n-1] coins to get sum "sum" +int count(int coins[], int n, int sum) +{ + + // If sum is 0 then there is 1 solution + // (do not include any coin) + if (sum == 0) + return 1; + + // If sum is less than 0 then no + // solution exists + if (sum < 0) + return 0; + + // If there are no coins and sum + // is greater than 0, then no + // solution exist + if (n <= 0) + return 0; + + // count is sum of solutions (i) + // including coins[n-1] (ii) excluding coins[n-1] + return count(coins, n - 1, sum) + + count(coins, n, sum - coins[n - 1]); +} + +// Driver code +int main() +{ + int i, j; + int coins[] = { 1, 2, 3 }; + int n = sizeof(coins) / sizeof(coins[0]); + int sum = 4; + + cout << " " << count(coins, n, sum); + + return 0; +} + +// This code is contributed by shivanisinghss2110