Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1509b3d

Browse files
Merge pull request #242 from nandikeshsingh/master
Coin change problem
2 parents 4505b18 + df3c086 commit 1509b3d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Problem :-
2+
===
3+
Given a value N and the values of different denomination,find the number of ways to make changes for N cents,if we have infinite supply of all denominations.
4+
5+
Input:-
6+
---
7+
Size of an array and all the values in the array and the number of cents.
8+
9+
Output :-
10+
---
11+
An integer that denotes the number of ways to make change.
12+
13+
#### Language : `C++`
14+
15+
#### Algorithm Paradigm : `Dynamic Programming`
16+
17+
#### Time Complexity : `O(N*M)`
18+
19+
#### Space Complexity : `O(N*M)`
20+
21+
Working :-
22+
---
23+
An arr array stores the different currency values.
24+
Another array temp stores the best values for sub-problems.For eg: `temp[3][4]` stores the optimal value for the number of ways
25+
to make change if the number of cents(N) were 4 and we only had `arr[0]`,`arr[1]` and `arr[2]` as the different values of currency.
26+
27+
By using dynamic programming we are bringing the time complexity down from exponentional(in case of brute force solution)
28+
to polynomial.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//Coin change problem using dynamic programming in C++.
2+
#include<bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
int M;//Variable to store the number of different currency values.
9+
cout<<"Enter the number of denominations : ";
10+
cin>>M;//Inputting the number of different currency value from user.
11+
cout<<endl;
12+
13+
int arr[M];//Array to store different currency values.
14+
for(int i=0;i<M;i++)
15+
{
16+
cout<<"Enter the value of denominaion "<<i+1<<" : ";
17+
cin>>arr[i];//Inputting the value of each currency from user.
18+
}
19+
cout<<endl;
20+
21+
int N;//Variable to store the number of cents whose number of ways to make change is to be found.
22+
cout<<"Enter the number of cents : ";
23+
cin>>N;//Inputting the number of cents from user.
24+
cout<<endl;
25+
26+
/*2D array to store the optimal value for sub-cases.*/
27+
int temp[M+1][N+1];
28+
29+
for(int i=0;i<M+1;i++)//Implementing the algorithm.
30+
{
31+
for(int j=0;j<N+1;j++)
32+
{
33+
if(i==0 || j==0)
34+
temp[i][j]=0;
35+
else if(j<arr[i-1])
36+
temp[i][j]=temp[i-1][j];
37+
else if(j==arr[i-1])
38+
temp[i][j]=1+temp[i-1][j];
39+
else
40+
temp[i][j]=temp[i-1][j]+temp[i][j-arr[i-1]];
41+
}
42+
}
43+
44+
cout<<"Number of ways to make change for "<<N<<" cent is "<<temp[M][N]<<endl;//Printing the final answer.
45+
46+
return 0;
47+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /