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
abhaykumar92893 days ago
class Solution {   public long count(int S[], int m, int n) {       // code here.       long dp[]=new long[n+1];       dp[0]=1;              for(int i=0;i<S.length;i++){//Iterate to each coin            for(int j=S[i];j<dp.length;j++){// fill or dp for each value               dp[j]+=dp[j-S[i]];           }       }       return dp[n];   }}
0
akshatsaxenaanpas3 days ago
long long int count(int S[], int m, int n) {

      
            long long dp[n+1] = {0};
        dp[0] = 1;
        
        for(int i=0;i<m;i++){
            int count = 1;
            int coin = S[i];
            for(int j=1;j<n+1;j++){
                if(coin<=j){
                   int rem = j-coin;
                       if(dp[rem]){
                           dp[j] += dp[rem];
                       }
                       
                    }
                }
                
            }
        return dp[n];
        
        
    }
+1
milindprajapatmst195 days ago
# define ll long long
const int N = 1e3;
ll dp[N + 1];

class Solution {
  public:
    ll count(int S[], int n, int m) {
        memset(dp, 0, sizeof(dp));
        dp[0] = 1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= m; j++) {
                if (j + S[i] <= m)
                    dp[j + S[i]] += dp[j];
            }
        }
        return dp[m];
    }
};
0
shivamshuklashivam291 week ago
long long int count(int S[], int m, int n) {

        // code here.
        if(m == 0 || n == 0) return 0;
        
        long long int dp[m+1][n+1];
        
        for(int i=0;i<m+1;i++){
            for(int j = 0;j<n+1;j++){
                if(i == 0) dp[i][j] = 0;
                if(j == 0) dp[i][j] = 1;
            }
        }
        for(int i=1;i<m+1;i++){
            for(int j = 1;j<n+1;j++){
                if(S[i-1] <= j){
                    dp[i][j] = dp[i][j-S[i-1]] + dp[i-1][j];
                }
                else dp[i][j] = dp[i-1][j];
            }
        }
      return dp[m][n];
    }
0
shivamshuklashivam29
This comment was deleted.
0
anuragshubham341 week ago
class Solution {   public long count(int s[], int m, int n) {     long dp[]=new long[n+1];     Arrays.fill(dp,0);        dp[0]=1;             for(int i=0;i<m;i++){         for(int j=s[i];j<=n;j++){             dp[j]+=dp[j-s[i]];         }     }     return dp[n];   }}
0
devashishbakare2 weeks ago
Java Dp solution (Tabulation)
class Solution {
    public long count(int arr[], int m, int target) {
        
        //created dp array for target size+1
        long dp[] = new long[target+1];
        
        //self position is 1, pow(x, 0) -> 1
        dp[0] = 1;
        
        //travers form total coins
        for(int i = 0 ; i < arr.length; i++)
        {
            //travels to given sum+1
            for(int j = arr[i]; j < dp.length; j++)
            {
                dp[j] += dp[j - arr[i]];
            }
        }
        
        //return total way to spend coin till give sum
        return dp[target];
    }
}
0
yasharthkatiyar2 weeks ago
JAVA Soln :-(Unbounded Knapsack)
 
class Solution {   public long count(int s[], int m, int n)   {    long dp[][]=new long [m+1][n+1];    for(int i=0;i<n+1;i++)    dp[0][i]=0;    for(int i=0;i<m+1;i++)    dp[i][0]=1;    for(int i=1;i<m+1;i++)    {        for(int j=1;j<n+1;j++)        {            if(s[i-1]<=j)            dp[i][j]=dp[i-1][j]+dp[i][j-s[i-1]];            else            dp[i][j]=dp[i-1][j];        }    }    return dp[m][n];   }}
0
abhiswc292 weeks ago
Python UnBounded Knapsack Variation
class Solution:
    def count(self, S, m, n): 
        # code here
        dp=[[0 for _ in range(n+1)]for _ in range(m+1)]
        dp[0][0]=1
        for i in range(1,m+1):
            dp[i][0]=1
        for i in range(1,n+1):
            dp[0][i]=0
        for i in range(1,m+1):
            for j in range(1,n+1):
                if S[i-1]<=j:
                    dp[i][j]=dp[i-1][j]+dp[i][j-S[i-1]]
                else:
                    dp[i][j]=dp[i-1][j]
        return dp[m][n]
-1
karanpratapsingh100012 weeks ago
long long int count(int S[], int n, int s) {

        // code here.
        //😘😉😎💕using dp
        if(n==0) return 0;
        
        vector<long long> dp(s+1,0);
        dp[0]=1;
        
        for(int i=1;i<=n;i++)
        {
            for(int sum=1;sum<=s;sum++)
            {
                if(S[i-1]<=sum) dp[sum]+=dp[sum-S[i-1]];
            }
        }
        
        return dp[s];
    }
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.