Coin Change | Practice | GeeksforGeeks

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. 

Example 1:
Input:
n = 4 , m = 3
S[] = {1,2,3}
Output: 4
Explanation: Four Possible ways are:
{1,1,1,1},{1,1,2},{2,2},{1,3}.

Example 2:
Input:
n = 10 , m = 4
S[] ={2,5,3,6}
Output: 5
Explanation: Five Possible ways are:
{2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} 
and {5,5}.


Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which accepts an array S[] its size m and n as input parameter and returns the number of ways to make change for N cents.

Expected Time Complexity: O(m*n).
Expected Auxiliary Space: O(n). 

Constraints:
1 <= n,m <= 103
0
savr123in 11 hours
C++  SOLUTION USING DP[MEMO+REC]
long long int f(int i,int num[], int x,vector<vector<long long>> &dp){     if(i==0)   {        if(x%num[0]==0) return 1;        else return 0;   }   if(dp[i][x]!=-1)          return dp[i][x];     long long int pick=0;     if(x>=num[i]) pick=f(i,num,x-num[i],dp);  long long int notpick=f(i-1,num,x,dp);
   return dp[i][x]=(pick+notpick);}long long int count(int S[], int m, int n)
 {
       vector<vector<long long>> dp(m,vector<long long>(n+1,-1));     return f(m-1,S,n,dp);   }
0
mishraaman4351 hour ago
JAVA
class Solution {
    public long count(int S[], int m, int n) {
        Arrays.sort(S);
        long []v = new long [n+1];
        v[0]=1;
        for(int i=0; i<m; i++){
            int j=S[i];
            while(j<=n){
                v[j]=v[j]+v[j-S[i]];
                j++;
            }
        }
        return v[n];
    }
}
0
kvatsadeo5 hours ago
  public:
    long long int f(int ind,int tar,int arr[],vector<vector<long long int>> &dp){
        if(ind==0) return ((tar%arr[ind])==0);
        if(dp[ind][tar]!=-1) return dp[ind][tar];
        if(arr[ind]<=tar){
            return dp[ind][tar]=f(ind,tar-arr[ind],arr,dp)+f(ind-1,tar,arr,dp);
        }
        return dp[ind][tar]=f(ind-1,tar,arr,dp);
    }
    long long int count(int arr[], int m, int n) {
        return f(m-1,n,arr,dp);
    }
    
    
    tabulation:
    vector<vector<long long int>> dp(m,vector<long long int>(n+1,0));
        
        for(int tar=0;tar<=n;tar++){
            dp[0][tar]=((tar%arr[0])==0);
        }
        for(int ind=1;ind<m;ind++){
            for(int tar=0;tar<=n;tar++){
                 if(arr[ind]<=tar){
                    dp[ind][tar]=dp[ind][tar-arr[ind]]+dp[ind-1][tar];
                }else dp[ind][tar]=dp[ind-1][tar];
            }
        }
        return dp[m-1][n];
 
 
 
+1
shobit15024 days ago
Easy solution c++
	long long int dp[1001][1001];
    long long int solve(int s[],int &m,int n,int p){
        
        if(n==0)return 1;
        if(n<0 or p==m)return 0;
        if(dp[n][p]!=-1)return dp[n][p];
        long long int ans=0;
        
        ans+=solve(s,m,n-s[p],p);
        ans+=solve(s,m,n,p+1);
        
        return dp[n][p]=ans;
    }
    long long int count(int S[], int m, int n) 										  {
        memset(dp,-1,sizeof dp);
        return solve(S,m,n,0);
    }
0
dev864405 days ago
//Recursion

long long int sol(int nums[],int m,int tar,int i){
     if(tar==0)return 1;
     if(i==0||tar<0)return 0;
     
     if(nums[i-1]<=tar)return sol(nums,m,tar-nums[i-1],i)+sol(nums,m,tar,i-1);
     else return sol(nums,m,tar,i-1);
 }
 
 //Tabulation
   long long int count(int nums[], int m, int n) {
       vector<long long int>dp(n+1,0);
       dp[0]=1;
       
       for(int i=1;i<m+1;i++){
           vector<long long int>temp(n+1,0);
           temp[0]=1;
           for(int j=1;j<n+1;j++){
               if(nums[i-1]<=j) temp[j]=temp[j-nums[i-1]]+dp[j];
               else temp[j]=dp[j];
           }
           dp=temp;
       }
       return dp[n];
   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.