The Coin Change Problem


Problem Statement :


Given an amount and the denominations of coins available, determine how many ways change can be made for amount. There is a limitless supply of each coin type.

Example
n = 3
c = [8,3,1,2]
There are 3 ways to make change for n=3: {1,1,1}, {1,2}, and {3}.

Function Description

Complete the getWays function in the editor below.

getWays has the following parameter(s):

   int n: the amount to make change for
   int c[m]: the available coin denominations

Returns

   int: the number of ways to make change

Input Format

The first line contains two space-separated integers n and m, where:
n is the amount to change
m is the number of coin types
The second line contains m space-separated integers that describe the values of each coin type.

Constraints

   1 <= c[i] <= 50
   1 <= n <= 250
   1 <= m <= 50
   Each c[i] is guaranteed to be distinct.



Solution :



title-img


                            Solution in C :

In C++ :




#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>

using namespace std;


int main() {
	vector<int> vec;
	int n;
	
	string str;
	getline(cin, str);
	
	const char * temp = str.c_str();	
	char * cstr = new char[10000];
	strcpy(cstr, temp);
	
	cstr = strtok(cstr, ", ");

	while(cstr!=NULL) {
		vec.push_back(atoi(cstr));
		cstr = strtok(NULL, ", ");
	}

	cin>>n;

	sort(vec.begin(), vec.end());
	int M = vec.size();
	vector< vector<int> > mat(M, vector<int>(n+1, 0));
	for(int i=0; i<M; i++)
		mat[i][0] = 1;
	for(int i=1; i<=n; i++)
		mat[0][i] = (i%vec[0]==0) ? 1 : 0;

	for(int i=1; i<M; i++) {
		for(int j=1; j<=n; j++) {
			mat[i][j]=mat[i-1][j];
			if(j>=vec[i])
				mat[i][j]+=mat[i][j-vec[i]];
		}
	}

	cout << mat[M-1][n] << endl;
	return 0;
}









In Java :




import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
    public static int ways(int[] a, int n){
        int len = a.length;
        int[] dp = new int[n+1];
        dp[0] = 1;
        
        for(int i=0;i<len;i++)
            for(int j=a[i];j<=n;j++)
                dp[j]+=dp[j-a[i]];
        
        return dp[n];
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        int n = Integer.parseInt(sc.nextLine().replace(" ",""));
        line = line.replace(" ","");
        String[] temp = line.split(",");
        int[] coins = new int[temp.length];
        for(int i=0;i<temp.length;i++)
            coins[i] = Integer.parseInt(temp[i]);
        
        System.out.println(ways(coins, n));
    }
}









In c :





#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
    char s[500];
    int a[50],k=0,i,j,n,l,x;
    gets(s);
    scanf("%d",&n);
    static int t[251];
    l=strlen(s);
    for(i=0;i<l;i++)
    {
        x=0;
        if(s[i]<='9'&&s[i]>='0')
            x=x*10+s[i]-'0';
        i++;
        if(i<n&&s[i]<='9'&&s[i]>='0'){
            x=x*10+s[i++]-'0';}
        i++;
            a[k++]=x;
    }
    t[0]=1;
    for(i=0;i<k;i++)
        for(j=a[i];j<=n;j++)
            t[j]+=t[j-a[i]];
    printf("%d",t[n]);
    return 0;
}









In Python3 :





cs = [int(i) for i in input().strip().split(',')]  # coin denominations
n = int(input())  # value for which we shall make change
ways = [0 for i in range(n + 1)]
ways[0] = 1

for i in range(len(cs)):
    for j in range(cs[i], n + 1):
        ways[j] += ways[j - cs[i]]

print(ways[n])
                        








View More Similar Problems

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →