Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on DP
  • Practice DP
  • MCQs on DP
  • Tutorial on Dynamic Programming
  • Optimal Substructure
  • Overlapping Subproblem
  • Memoization
  • Tabulation
  • Tabulation vs Memoization
  • 0/1 Knapsack
  • Unbounded Knapsack
  • Subset Sum
  • LCS
  • LIS
  • Coin Change
  • Word Break
  • Egg Dropping Puzzle
  • Matrix Chain Multiplication
  • Palindrome Partitioning
  • DP on Arrays
  • DP with Bitmasking
  • Digit DP
  • DP on Trees
  • DP on Graph
Open In App
Next Article:
Coin change problem with limited coins
Next article icon

Coin change problem with limited coins

Last Updated : 09 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given three integers n, k, target, and an array of coins[] of size n. Find if it is possible to make a change of target cents by using an infinite supply of each coin but the total number of coins used must be exactly equal to k.

Examples:

Input: n = 5, k = 3, target = 11, coins = {1, 10, 5, 8, 6}
Output: 1
Explanation: 2 coins of 5 and 1 coin of 1 can be used to make a change of 11 i.e. 11 => 5 + 5 + 1.

Input: n = 3, k = 5, target = 25, coins = {7, 2, 4}
Output: 1
Explanation: 3 coins 7, 2 coins of 2 can be used to make a change of 25 i.e. 25 => 7+7+7+2+2.

Naive Approach :

The basic way to solve this problem is to generate all possible combinations by using a recursive approach.

Below are the steps involved in the implementation of the code:

  • If target == 0 and k == 0, return true
  • If n == 0 or target < 0 or k < 0, return false 
  • Otherwise, the result is true if either of the following conditions is satisfied:
    • Include the last coin and recursively check if it is possible to make the remaining target using k-1 coins.
    • Exclude the last coin and recursively check if it is possible to make the target using the remaining n-1 coins and k coins.

below is the code implementation of the above code:

C++
#include <bits/stdc++.h> using namespace std;  // Function to check if it is possible to make a change of // target cents using exactly k coins from the given coins // array of size n bool canMakeChange(int target, int k, int coins[], int n) {     // If target and k are both 0, then we have found a     // valid combination of coins     if (target == 0 && k == 0) {         return true;     }     // If n becomes 0 or target or k becomes negative, then     // the current combination of coins is not valid     if (n == 0 || target < 0 || k < 0) {         return false;     }     // we can either include the current coin or exclude it     // We check if it is possible to make the change by     // including the current coin or excluding it     return canMakeChange(target - coins[n - 1], k - 1,                          coins, n)            || canMakeChange(target, k, coins, n - 1); }  int main() {     int n = 5;     int k = 3;     int target = 11;     int coins[] = { 1, 10, 5, 8, 6 };      // Check if it is possible to make the change using 'k'     // coins from the 'coins' array to get 'target' cents     bool result = canMakeChange(target, k, coins, n);      // Print the result     if (result) {         cout << "1" << endl;     }     else {         cout << "0" << endl;     }     return 0; } 
Java
import java.util.Arrays;  class GFG {          // Function to check if it is possible to make a change of     // target cents using exactly k coins from the given coins     // array of size n     static boolean canMakeChange(int target, int k, int[] coins, int n) {         // If target and k are both 0, then we have found a         // valid combination of coins         if (target == 0 && k == 0) {             return true;         }         // If n becomes 0 or target or k becomes negative, then         // the current combination of coins is not valid         if (n == 0 || target < 0 || k < 0) {             return false;         }         // we can either include the current coin or exclude it         // We check if it is possible to make the change by         // including the current coin or excluding it         return canMakeChange(target - coins[n - 1], k - 1, coins, n)                 || canMakeChange(target, k, coins, n - 1);     }          public static void main(String[] args) {         int n = 5;         int k = 3;         int target = 11;         int[] coins = { 1, 10, 5, 8, 6 };          // Check if it is possible to make the change using 'k'         // coins from the 'coins' array to get 'target' cents         boolean result = canMakeChange(target, k, coins, n);          // Print the result         if (result) {             System.out.println("1");         } else {             System.out.println("0");         }     } }  // This code is contributed by shivamgupta0987654321 
Python
# python Implementation  # Function to check if it is possible to make a change of # target cents using exactly k coins from the given coins # array of size n def can_make_change(target, k, coins, n):     # If target and k are both 0, then we have found a     # valid combination of coins     if target == 0 and k == 0:         return True     # If n becomes 0 or target or k becomes negative, then     # the current combination of coins is not valid     if n == 0 or target < 0 or k < 0:         return False     # we can either include the current coin or exclude it     # We check if it is possible to make the change by     # including the current coin or excluding it     return can_make_change(target - coins[n - 1], k - 1, coins, n) or can_make_change(target, k, coins, n - 1)  def main():     n = 5     k = 3     target = 11     coins = [1, 10, 5, 8, 6]      # Check if it is possible to make the change using 'k'     # coins from the 'coins' array to get 'target' cents     result = can_make_change(target, k, coins, n)      # Print the result     if result:         print("1")     else:         print("0")  if __name__ == "__main__":     main() 
C#
// C# Implementation using System;  class GFG {      // Function to check if it is possible to make a change of // target cents using exactly k coins from the given coins // array of size n static bool canMakeChange(int target, int k, int[] coins, int n) {     // If target and k are both 0, then we have found a     // valid combination of coins     if (target == 0 && k == 0) {         return true;     }     // If n becomes 0 or target or k becomes negative, then     // the current combination of coins is not valid     if (n == 0 || target < 0 || k < 0) {         return false;     }     // we can either include the current coin or exclude it     // We check if it is possible to make the change by     // including the current coin or excluding it     return canMakeChange(target - coins[n - 1], k - 1,                         coins, n)         || canMakeChange(target, k, coins, n - 1); }  public static int Main() {     int n = 5;     int k = 3;     int target = 11;     int[] coins = { 1, 10, 5, 8, 6 };      // Check if it is possible to make the change using 'k'     // coins from the 'coins' array to get 'target' cents     bool result = canMakeChange(target, k, coins, n);      // Print the result     if (result) {         Console.WriteLine("1");     }     else {         Console.WriteLine("0");     }     return 0; }  }  // This code is contributed by Utkarsh Kumar 
JavaScript
// JavaScript Implementation  // Function to check if it is possible to make a change of // target cents using exactly k coins from the given coins // array of size n function can_make_change(target, k, coins, n) {     // If target and k are both 0, then we have found a     // valid combination of coins     if (target === 0 && k === 0) {         return true;     }     // If n becomes 0 or target or k becomes negative, then     // the current combination of coins is not valid     if (n === 0 || target < 0 || k < 0) {         return false;     }     // we can either include the current coin or exclude it     // We check if it is possible to make the change by     // including the current coin or excluding it     return can_make_change(target - coins[n - 1], k - 1, coins, n) || can_make_change(target, k, coins, n - 1); }  let n = 5; let k = 3; let target = 11; let coins = [1, 10, 5, 8, 6];  // Check if it is possible to make the change using 'k' // coins from the 'coins' array to get 'target' cents let result = can_make_change(target, k, coins, n);  // Print the result if (result) {     console.log("1"); } else {     console.log("0"); }  // This code is contributed by Tapesh(tapeshdua420) 

Output
1

Time Complexity : O(2^(k+n))
Space Complexity :  O(k+n)

Efficient Approach: This can be solved with the following idea:

The approach used is a dynamic programming approach, The approach works by building a table of subproblems using a two-dimensional boolean array. The approach iterates over all values of i from 1 to target and all values of j from 0 to K. For each subproblem dp[i][j], the algorithm checks whether any of the coins in the array can be used to add up to i while using j-1 coins to add up to the remaining value. If so, the subproblem is marked as solved by setting dp[i][j] = true. The algorithm returns dp[target][K] as the solution to the original problem. 

Below are the steps involved in the implementation of the code:

  • Initialize a two-dimensional boolean array dp of size (target+1) x (K+1).
  • Set the base case dp[0][0] = true.
  • For i from 1 to target and j from 0 to K, do the following:
    • For each coin in the array coins, do the following:
      • If the current coin denomination is less than or equal to i, and j > 0 (i.e., there are still coins remaining to be used), and dp[i-coin][j-1] is true, then set dp[i][j] to true and break out of the loop over coins.
  • Return dp[target][K] as the solution to the original problem.

below is the code implementation of the above code:

C++
// Nikunj Sonigara  #include <bits/stdc++.h> using namespace std;  // Function to check whether target can be achieved by K coins bool makeChanges(int N, int K, int target, vector<int>& coins) {     vector<vector<bool>> dp(target + 1, vector<bool>(K + 1, false));     dp[0][0] = true;      for (int i = 1; i <= target; i++) {         for (int j = 0; j <= K; j++) {             for (int coin : coins) {                 if (coin <= i && j > 0 && dp[i - coin][j - 1]) {                     dp[i][j] = true;                     break;                 }             }         }     }      // Return the result     return dp[target][K]; }  int main() {     int N = 5;     int K = 3;     int target = 11;     vector<int> coins = {1, 10, 5, 8, 6};      // Function call     bool result = makeChanges(N, K, target, coins);     if (result) {         cout << '1' << endl;     } else {         cout << '0' << endl;     }      return 0; } 
Java
// java code for the above approach: import java.util.*;  public class CoinChangeProblem {      // Function to check whether target     // can be achieved by K coins     public static boolean     makeChanges(int N, int K, int target, int[] coins)     {         boolean[][] dp = new boolean[target + 1][K + 1];         dp[0][0] = true;          for (int i = 1; i <= target; i++) {             for (int j = 0; j <= K; j++) {                 for (int coin : coins) {                     if (coin <= i && j > 0                         && dp[i - coin][j - 1]) {                         dp[i][j] = true;                         break;                     }                 }             }         }          // Return the result         return dp[target][K];     }      // Driver codes     public static void main(String[] args)     {         int N = 5;         int K = 3;         int target = 11;         int[] coins = { 1, 10, 5, 8, 6 };          // Function call         boolean result = makeChanges(N, K, target, coins);         if (result == true) {             System.out.println('1');         }         else {             System.out.println('0');         }     } } 
Python
# Nikunj Sonigara  def make_changes(N, K, target, coins):     dp = [[False for _ in range(K + 1)] for _ in range(target + 1)]     dp[0][0] = True      for i in range(1, target + 1):         for j in range(K + 1):             for coin in coins:                 if coin <= i and j > 0 and dp[i - coin][j - 1]:                     dp[i][j] = True                     break      # Return the result     return dp[target][K]  N = 5 K = 3 target = 11 coins = [1, 10, 5, 8, 6]  # Function call result = make_changes(N, K, target, coins) if result:     print('1') else:     print('0') 
C#
using System;  public class CoinChangeProblem {     // Function to check whether target     // can be achieved by K coins     public static bool MakeChanges(int N, int K, int target, int[] coins)     {         bool[,] dp = new bool[target + 1, K + 1];         dp[0, 0] = true;         for (int i = 1; i <= target; i++)         {             for (int j = 0; j <= K; j++)             {                 foreach (int coin in coins)                 {                     if (coin <= i && j > 0 && dp[i - coin, j - 1])                     {                         dp[i, j] = true;                         break;                     }                 }             }         }         // Return the result         return dp[target, K];     }     // Driver codes     public static void Main(string[] args)     {         int N = 5;         int K = 3;         int target = 11;         int[] coins = { 1, 10, 5, 8, 6 };         // Function call         bool result = MakeChanges(N, K, target, coins);         if (result == true)         {             Console.WriteLine('1');         }         else         {             Console.WriteLine('0');         }     } } 
JavaScript
// Nikunj Sonigara  function makeChanges(N, K, target, coins) {     const dp = new Array(target + 1);     for (let i = 0; i <= target; i++) {         dp[i] = new Array(K + 1).fill(false);     }     dp[0][0] = true;      for (let i = 1; i <= target; i++) {         for (let j = 0; j <= K; j++) {             for (const coin of coins) {                 if (coin <= i && j > 0 && dp[i - coin][j - 1]) {                     dp[i][j] = true;                     break;                 }             }         }     }      // Return the result     return dp[target][K]; }  const N = 5; const K = 3; const target = 11; const coins = [1, 10, 5, 8, 6];  // Function call const result = makeChanges(N, K, target, coins); if (result) {     console.log('1'); } else {     console.log('0'); } 

Output
1

Time Complexity: O(NKT)
Auxiliary Space: O(NKT)


Next Article
Coin change problem with limited coins

S

shivammiglani09
Improve
Article Tags :
  • Dynamic Programming
  • DSA
  • Arrays
Practice Tags :
  • Arrays
  • Dynamic Programming

Similar Reads

    Coin Change - Minimum Coins to Make Sum
    Given an array of coins[] of size n and a target value sum, where coins[i] represent the coins of different denominations. You have an infinite supply of each of the coins. The task is to find the minimum number of coins required to make the given value sum. If it is not possible to form the sum usi
    15+ min read
    Coin Change - Count Ways to Make Sum
    Given an integer array of coins[] of size n representing different types of denominations and an integer sum, the task is to count all combinations of coins to make a given value sum. Note: Assume that you have an infinite supply of each type of coin. Examples: Input: sum = 4, coins[] = [1, 2, 3]Out
    15+ min read
    Buy minimum items without change and given coins
    You have an unlimited number of 10-rupee coins and exactly one coin of r rupee and you need to buy minimum items each of cost k such that you do not ask for change.Examples: Input: k = 15, r = 2 Output: 2 You should buy two cables and pay 2*15=30 rupees. It is obvious that you can pay this sum witho
    4 min read
    Frobenius coin problem
    Given two coins of denominations "x" and "y" respectively, find the largest amount that cannot be obtained using these two coins (assuming an infinite supply of coins) followed by the total number of such non-obtainable amounts, if no such value exists print "NA".Examples : Input : x = 2, y = 5 Outp
    6 min read
    Fixed Coins for Target Sum with Constraint K
    Given a set of coin denominations the total number of coins (n) and a target amount (target), the task is to determine if it's possible to make change, for the target amount using k coins. You have one supply of each coin denomination. Examples: Input: n = 4, k = 2, Target = 9, Coins [2, 3, 5, 7]Out
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences