Number of Coins | Practice | GeeksforGeeks

Given a value V and array coins[] of size M, the task is to make the change for V cents, given that you have an infinite supply of each of coins{coins1, coins2, ..., coinsm} valued coins. Find the minimum number of coins to make the change. If not possible to make change then return -1.

Example 1:
Input: V = 30, M = 3, coins[] = {25, 10, 5}
Output: 2
Explanation: Use one 25 cent coin
and one 5 cent coin
Input: V = 11, M = 4,coins[] = {9, 6, 5, 1} 
Output: 2 
Explanation: Use one 6 cent coin
and one 5 cent coin
0
aamitprasad618in 12 hours
CPP
 
int minCoins(int arr[], int n, int sum) 
	{ 
	    int dp[n+1][sum+1];
	    
	    for(int i=0;i<=n;i++){
	        dp[i][0]=0;
	    }
	    for(int j=1;j<=sum;j++){
	        dp[0][j]=INT_MAX-1;
	    }
	    
	    for(int j=1;j<=sum;j++){
	        if(j%arr[0]==0){
	            dp[1][j]=j/arr[0];
	        }
	        else{
	            dp[1][j]=INT_MAX-1;
	        }
	    }
	    
	    for(int i=2;i<=n;i++){
	        for(int j=1;j<=sum;j++){
	            if(arr[i-1] <= j){
	                dp[i][j]=min(dp[i][j-arr[i-1]]+1,0+dp[i-1][j]);
	            }
	            else{
	                dp[i][j]=dp[i-1][j];
	            }
	        }
	    }
	    
	    if(dp[n][sum]==INT_MAX-1){
	        return -1;
	    }
	    else{
	        return dp[n][sum];
	    }
	    
	} 
0
anandpatil44053 days ago
Easy C++ code
 
vector<int>dp(V+1,INT_MAX);    dp[0] = 0;    for(int i=0;i<M;i++)    {        for(int j=1;j<=V;j++)        {            if(j-coins[i] >=0)            {                if(dp[j-coins[i]] != INT_MAX)                dp[j] = min(dp[j],1+dp[j-coins[i]]);            }        }    }    return dp[V] ==INT_MAX ? -1:dp[V];
0
chessnoobdj4 days ago
C++ space optimized
int minCoins(int coins[], int M, int target) 
{
	vector <long> dp(target+1, INT_MAX);
	dp[0] = 0;
	for(int i=0; i<M; i++){
	    for(int j=1; j<=target; j++){
	        if(j-coins[i] >= 0)
	            dp[j] = min(dp[j], 1+dp[j-coins[i]]);
	    }
	}
	return dp[target] == INT_MAX ? -1 : dp[target];
} 
0
parvathirajesh20015 days ago
// Simple and the best CPP soln
 
int dp[M+1][V+1];
       
       for(int i=0;i<=M;++i)
       {
           for(int j=0;j<=V;++j)
           {
               if(j==0)
                   dp[i][j] = 0;
               else if(i==0)
                   dp[i][j] = INT_MAX-1;
               else if(coins[i-1]>j)
                   dp[i][j] = dp[i-1][j];
               else
                   dp[i][j] = min(1 + dp[i][j-coins[i-1]], dp[i-1][j]);
           }
       }
        if(dp[M][V] == INT_MAX-1) 
        return -1;
        
        return dp[M][V];
   }
0
akkeshri140420011 week ago
int minCoins(int coins[], int M, int V) 
{ 
    // Your code goes here
    int n=M;
    int W=V;
    vector<vector<int>>dp(n+1,vector<int>(W+1));
    for(int i=0;i<n+1;i++){
        for(int j=0;j<W+1;j++){
            if(i==0 and j>0){
                dp[i][j]=INT_MAX-1;
            }
            else if(j==0){
                dp[i][j]=0;
            }
        }
    }
    for(int j=1;j<W+1;j++){
        if(j%coins[0]==0){
            dp[1][j]=j/coins[0];
        }
        else{
            dp[1][j]=INT_MAX-1;
        }
    }
    for(int i=2;i<n+1;i++){
        for(int j=1;j<W+1;j++){
            if(j>=coins[i-1]){
            dp[i][j]=min(1+dp[i][j-coins[i-1]],dp[i-1][j]);
            }
            else{
                dp[i][j]=dp[i-1][j];
            }
        }
    }
    return dp[n][W]=(dp[n][W]==INT_MAX-1)?-1:dp[n][W];
} 
  
0
tahabasra921 week ago
c++ recursion + memoization
 
 
public:int solve(int coins[],int i,int &V,int &M,int curr_v,vector<vector<int>> &dp){    if(curr_v==V){        return 0;    }    if(curr_v>V){        return 1e8;    }    if(i==M){        return 1e8;    }    if(dp[i][curr_v]!=-1)    {        return dp[i][curr_v];    }        int a=1+solve(coins,i,V,M,curr_v+coins[i],dp);    int b=solve(coins,i+1,V,M,curr_v,dp);    return dp[i][curr_v]=min(a,b);}int minCoins(int coins[], int M, int V) {     // Your code goes here    vector<vector<int>> dp(M,vector<int>(V+1,-1));        int ans=solve(coins,0,V,M,0,dp);    if( ans==1e8){        return -1;    }else{        return ans;    }}   
0
riteshranka1 week ago
int minCoins(int coins[], int M, int V)    {               sort(coins, coins+M);       vector<int> dp(V+1, INT_MAX);       dp[0] = 0;       for(int i = 1; i <= V; i++){           int j = 0;           while(j < M && coins[j] <= i){               if(dp[i-coins[j]] != INT_MAX){                   dp[i] = min(dp[i], dp[i-coins[j]]+1);               }               j++;           }       }       if(dp[V] == INT_MAX) return -1;       return dp[V];} 
0
shivaraj52 weeks ago
int minCoins(int a[], int M, int V) {     // Your code goes here    int W=V;    int n=M;    int dp[n+1][W+1];    for(int i=0;i<=n;i++)     dp[i][0]=0;    for(int j=0;j<=W;j++)     dp[0][j]=INT_MAX-1;    for(int j=1;j<=W;j++){     if(j%a[0]==0)      dp[1][j]=j/a[0];     else      dp[1][j]=INT_MAX-1;    }    for(int i=2;i<=n;i++){     for(int j=1;j<=W;j++){      if(a[i-1]<=j)       dp[i][j]=min(dp[i-1][j],1+dp[i][j-a[i-1]]);      else       dp[i][j]=dp[i-1][j];     }    }    if(dp[n][W]==INT_MAX || dp[n][W]==INT_MAX-1)        return -1;    return dp[n][W];} 
0
ritikk51882 weeks ago
public: int solve(int index,int target,int coins[], vector<vector<int>> &dp){     if(dp[index][target]!=-1)        return(dp[index][target]);     if(index==0){         if(target%coins[0]==0)             return(target/coins[0]);       return(1e9);           }     int take=INT_MAX,notake=INT_MAX;         if(target>=coins[index])             take=1+solve(index,target-coins[index],coins,dp);         notake=solve(index-1,target,coins,dp);          return(dp[index][target]=min(take,notake)); }int minCoins(int coins[], int M, int V) {     vector<vector<int>> dp(M,vector<int>(V+1,-1));    vector<int> prev(V+1,0);    for(int i=0;i<M;i++){        vector<int> curr(V+1,0);        for(int j=0;j<=V;j++){            if(i==0){                if((j%coins[0])==0)                    curr[j]=(j/coins[0]);               else                    curr[j]=1e9;                     }            else{            int take=INT_MAX,notake=INT_MAX;            if(j>=coins[i])               take=1+curr[j-coins[i]];            notake=prev[j];            curr[j]=min(take,notake);        }        }        prev=curr;    }    //int ans=solve(M-1,V,coins,dp);    if(prev[V]>=1e9)       return(-1);    return prev[V];} 
+1
gobacktocode2 weeks ago
EASY UNDERSTANDING|| RECURSIVE+MEMOIZATION+TABULATION||
//recursive+memoization code :)int memo(int coins[],int n,int v,vector<vector<int>>& t){    if(v==0) return 0;    if(n==0){        if(v%coins[0]==0) return v/coins[0];        return 1e9;//not by INT_MAX bcz to reduce memory overflow 1+1+1...memo(coins,n,v-coins[n-1],t);      }    if(t[n][v]!=-1)    return t[n][v];    if(coins[n-1]<=v){        return t[n][v]= min(1+memo(coins,n,v-coins[n-1],t),0+memo(coins,n-1,v,t));    }    return t[n][v]=memo(coins,n-1,v,t);}
//tabulation code :)int tab(int coins[],int val,int n){   int t[n+1][val+1];     for(int i=0;i<n+1;i++){        t[i][0]=0;     }        for(int j=0;j<=val;j++){            //only this condition is needed ..we convert base case into initialization so don't write it directly            // it is formed from our memoized code in which the base case converted into initializtion condition           if(val%coins[0]==0)           {               t[0][j]=val/coins[0];           }        t[0][j]= 1e9;          }                    for(int i=1;i<=n;i++){              for(int j=1;j<=val;j++){                                    if(coins[i-1]<=j){                      t[i][j]= min(1+t[i][j-coins[i-1]],0+t[i-1][j]);                  }                  else                  t[i][j]=t[i-1][j];              }          }          return t[n][val];   }int minCoins(int coins[], int n, int v) {         vector<vector<int>> t(n+1,vector<int>(v+1,-1));
       //int ans =memo(coins,n,v,t);       int ans= tab(coins,v,n);   return ans>=1e9?-1:ans;} 
 
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.