0% found this document useful (0 votes)
5 views4 pages

Lab 10 - Ans

The document discusses counting all combinations of coins to achieve a given sum using dynamic programming. It presents both a recursive solution and a bottom-up dynamic programming approach, detailing the logic and providing C++ code examples for each method. The time complexities for the recursive and dynamic programming solutions are O(2^sum) and O(N*sum), respectively.

Uploaded by

1505196429
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Lab 10 - Ans

The document discusses counting all combinations of coins to achieve a given sum using dynamic programming. It presents both a recursive solution and a bottom-up dynamic programming approach, detailing the logic and providing C++ code examples for each method. The time complexities for the recursive and dynamic programming solutions are O(2^sum) and O(N*sum), respectively.

Uploaded by

1505196429
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Dynamic programming

Count all combinations of coins to make a given value sum


Given an integer array of coins[N] of size N representing different types of
denominations and an integer sum, the task is to count all combinations of coins to
make a given value sum.
Note: Assume that you have an infinite supply of each type of coin.

Exercise 1:
Input: sum = 4, coins[] = {1,2,3},
Answer:
Output: 4
Explanation: there are four solutions: {1, 1, 1, 1}, {1, 1, 2}, {2, 2}, {1, 3}.

Exercise 2:
Input: sum = 10, coins[] = {2, 5, 3, 6}
Answer:
Output: 5
Explanation: There are five solutions:
{2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}.

Recursive solution:
count(coins,n,sum) = count(coins,n,sum-count[n-1]) + count(coins,n-1,sum)
For each coin, there are 2 options.
- Include the current coin: Subtract the current coin’s denomination from the
target sum and call the count function recursively with the updated sum and
the same set of coins i.e., count(coins, n, sum – coins[n-1] )
- Exclude the current coin: Call the count function recursively with the same
sum and the remaining coins. i.e., count(coins, n-1,sum ).
The final result will be the sum of both cases.
Time Complexity: O(2sum)
Exercise 3: ask students to implement the recursive solution above, considering
base case and recursive case. (Students can use any language they want)
Answer:
// Recursive C++ program for
// coin change problem.
#include <bits/stdc++.h>
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, sum - coins[n - 1])
+ count(coins, n - 1, sum);
}

// Driver code
int main()
{
int i, j;
int coins[] = { 1, 2, 3 };
int n = sizeof(coins) / sizeof(coins[0]);
int sum = 5;

cout << " " << count(coins, n, sum);

return 0;
}

Bottom-up dynamic programming solution


- Create a 2D dp array with rows and columns equal to the number of coin
denominations and target sum.
- dp[0][0] will be set to 1 which represents the base case where the target sum
is 0, and there is only one way to make the change by not selecting any coin.
- Iterate through the rows of the dp array (i from 1 to n), representing the
current coin being considered.
o The inner loop iterates over the target sums (j from 0 to sum).
 Add the number of ways to make change without using the
current coin, i.e., dp[i][j] += dp[i-1][j].
 Add the number of ways to make change using the current coin,
i.e., dp[i][j] += dp[i][j-coins[i-1]].
- dp[i][j] will represent the number of distinct ways to make the sum j by
using the first i coin denominations.
Time complexity: O(N*sum)
C++ code:
#include <bits/stdc++.h>

using namespace std;


// Returns total distinct ways to make sum using n coins of
// different denominations
int count(vector<int>& coins, int n, int sum)
{
// 2d dp array where n is the number of coin
// denominations and sum is the target sum
vector<vector<int> > dp(n + 1, vector<int>(sum + 1, 0));

// Represents the base case where the target sum is 0,


// and there is only one way to make change: by not
// selecting any coin
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= sum; j++) {

// Add the number of ways to make change without


// using the current coin,
dp[i][j] += dp[i - 1][j];

if ((j - coins[i - 1]) >= 0) {

// Add the number of ways to make change


// using the current coin
dp[i][j] += dp[i][j - coins[i - 1]];
}
}
}
return dp[n][sum];
}

// Driver Code
int main()
{
vector<int> coins{ 1, 2, 3 };
int n = 3;
int sum = 5;
cout << count(coins, n, sum);
return 0;
}

You might also like