×

String Coding Problems

Arrays Coding Problems

Sorting Coding Problems

Searching Coding Problems

Coding Algorithms

Tree Coding Problems

Stack Coding Problems

Linked list Coding Problems

Graph Coding Problems

Greedy Algorithms Coding Problems

Dynamic Programming Coding Problems

Matrix Coding Problems

Recursion Coding Problems

Number Theory Coding Problems

Backtracking Coding Problems

Heap Coding Problems

Brute Force Coding Problems

Implementation Coding Problems

Google Contests

Competitive Programming Coding

Miscellaneous

Coin Change Problem With Solution

In this article, we are going to see how to solve the coin change problem? Which can be solved using dynamic programming concept. By Radib Kar Last updated : April 21, 2024

Problem statement

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.

Example:

Input:
N = 4 
S = {1, 2, 3} //infinite number of 1 cent, 2 cent, 3 cent coins

Output:
4
{1,1,1,1} //four 1 cent coins
{1,1,2} //two 1 cent coins, one 2 cent coins
{2,2} //two 2 cent coins 
{1,3} //one 1 cent and one 3 cent coins
Thus total four ways (Order doesn’t matter)

Solution of Coin Change Problem

Let's think of the solution. Let’s do it by hand first. An intuitive idea can be two checks whether the amount can be handled by the same valued coins.

Like 4 cent can be managed by four 1 cent coins.

Same time can be managed by two 2 cent coins.

For the rest, we need to take different valued coins.

Now we can think of memorization. We can simply store the sub-amounts that can have been managed still.

Like two 1 cent coin manage 2 cents. Thus 2 cent is our sub amount.

Now we pick two more 1 cent coins that will sum up to 4.

Else we can pick only one 2cent coin also summing up to 4.

This memorized approach helps us to solve the problem.

Let's revise the algorithm

Prerequisite: Coins array, amount

Algorithm

1.  Create a DP table of size amount
    Table[amount+1]={0};
2.  Base case table[0]=1
3.  For each coins[i] from the coins array
        For j= 1: amount //j be the sub-amount
            IF j>=coins[i] 
                Table[j]=table[j]+table[j-coins[i]]
            END IF
        END For
    END For
4.  Return table[amount]
Table[amount] refers to all possible way to make the amount

Explanation

We have pre added coin 0-cent as coin[0]=0
For Coin index:  1 //coins[1]=1

Sub amount:1

Table status:
1 | 1 | 0 | 0 | 0


Sub amount:2
Table status:
1 | 1 | 1 | 0 | 0

Sub amount:3

1 | 1 | 1 | 1 | 0
Sub amount:4
1 | 1 | 1 | 1 | 1 

This actually shows that by coin[1] all the sub-amounts can be managed only by one way, so if we continue this way for other coins we will get all the possible ways.


C++ implementation of Coin Change Problem

#include <bits/stdc++.h>
using namespace std;

void findway(vector<int> a, int n, int amount) {
  int table[amount + 1];  // DP table
  memset(table, 0, sizeof(table));
  table[0] = 1;
  // j be the sub-amounts
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= amount; j++) {
      if (j >= a[i]) table[j] += table[j - a[i]];
    }
  }
  cout << table[amount] << endl;  // final result
}

int main() {
  int n, item, amount;

  cout << "Enter the number of coins\n";
  scanf("%d", &n);

  cout << "Enter value of coins\n";
  vector<int> a;  // coins array
  // we pre add 0-cent coin as coins[0]
  // for sake of 1 indexing
  a.push_back(0);

  for (int j = 0; j < n; j++) {
    scanf("%d", &item);
    a.push_back(item);
  }

  cout << "Enter total amount\n";
  cin >> amount;
  cout << "Number of ways to sum the amount is: ";
  findway(a, n, amount);

  return 0;
}

Output

Enter the number of coins
3
Enter value of coins
1 2 3
Enter total amount
4
Number of ways to sum the amount is: 4

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.