Coin Change
PROBLEM :
Given a value N, find the number of ways to make change for N cents, if we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins. The order of coins doesn’t matter. For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, the first line contains an integer 'M' denoting the size of array. The second line contains M space-separated integers A1, A2, ..., AN denoting the elements of the array. The third line contains an integer 'N' denoting the cents.
Output:
Print number of possible ways to make change for N cents.
Constraints:
1 = T = 50
1 = N = 300
1<sizeof(A)<200
1 = A[i] = 300
Example:
Input:
2
3
1 2 3
4
4
2 5 3 6
10
Output:
4
5
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std ;
long long Coin_Change(int [],int ,int ) ;
int main()
{
int t,arr[200],i,no,k ;
long long ans ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cin>>k ;
ans=Coin_Change(arr,no,k) ;
cout<<ans<<endl ;
}
return 0 ;
}
long long Coin_Change(int arr[],int n,int m)
{
long long mat[n+1][m+1] ;
int i,j ;
for(i=0;i<=n;i++)
mat[i][0]=1 ;
for(j=0;j<=m;j++)
mat[0][j]=0 ;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(arr[i-1]>j)
mat[i][j]=mat[i-1][j] ;
else
mat[i][j]=mat[i-1][j]+mat[i][j-arr[i-1]] ;
}
}
return mat[n][m] ;
}
--------------------------------------------------------------------------------
BRTTER c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std ;
long long Coin_Change(int [],int ,int ) ;
int main()
{
int t,arr[200],i,no,k ;
long long ans ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cin>>k ;
ans=Coin_Change(arr,no,k) ;
cout<<ans<<endl ;
}
return 0 ;
}
long long Coin_Change(int a[],int n,int m)
{
long long arr[m+1]={0} ;
int i,j ;
arr[0]=1 ;
for(i=0;i<n;i++)
{
for(j=a[i];j<=m;j++)
{
arr[j]=arr[j]+arr[j-a[i]] ;
}
}
return arr[m] ;
}
---------------------------------------------------------------------------------
Given a value N, find the number of ways to make change for N cents, if we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins. The order of coins doesn’t matter. For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, the first line contains an integer 'M' denoting the size of array. The second line contains M space-separated integers A1, A2, ..., AN denoting the elements of the array. The third line contains an integer 'N' denoting the cents.
Output:
Print number of possible ways to make change for N cents.
Constraints:
1 = T = 50
1 = N = 300
1<sizeof(A)<200
1 = A[i] = 300
Example:
Input:
2
3
1 2 3
4
4
2 5 3 6
10
Output:
4
5
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std ;
long long Coin_Change(int [],int ,int ) ;
int main()
{
int t,arr[200],i,no,k ;
long long ans ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cin>>k ;
ans=Coin_Change(arr,no,k) ;
cout<<ans<<endl ;
}
return 0 ;
}
long long Coin_Change(int arr[],int n,int m)
{
long long mat[n+1][m+1] ;
int i,j ;
for(i=0;i<=n;i++)
mat[i][0]=1 ;
for(j=0;j<=m;j++)
mat[0][j]=0 ;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(arr[i-1]>j)
mat[i][j]=mat[i-1][j] ;
else
mat[i][j]=mat[i-1][j]+mat[i][j-arr[i-1]] ;
}
}
return mat[n][m] ;
}
--------------------------------------------------------------------------------
BRTTER c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std ;
long long Coin_Change(int [],int ,int ) ;
int main()
{
int t,arr[200],i,no,k ;
long long ans ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cin>>k ;
ans=Coin_Change(arr,no,k) ;
cout<<ans<<endl ;
}
return 0 ;
}
long long Coin_Change(int a[],int n,int m)
{
long long arr[m+1]={0} ;
int i,j ;
arr[0]=1 ;
for(i=0;i<n;i++)
{
for(j=a[i];j<=m;j++)
{
arr[j]=arr[j]+arr[j-a[i]] ;
}
}
return arr[m] ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment