Questions tagged [coin-change]

All problems (regardless of currency) with making change from a given amount of currency into a number of coins and bills of varying denominations.

All problems with making change from a given amount of currency into a number of coins and bills of varying denominations.

For example, finding the correct number of $20, $10, $5, $1 bills and $0.25, $0.10, $0.05, and $0.01 coins that add up to a given sum.

Variations of the problem are to find any solution, all the solutions, the one with the least number of coins / bills, or with a specific number of them.

225 questions
116
votes
37 answers

How to find all combinations of coins when given some dollar value

I found a piece of code that I was writing for interview prep few months ago. According to the comment I had, it was trying to solve this problem: Given some dollar value in cents (e.g. 200 = 2 dollars, 1000 = 10 dollars), find all the combinations…
codingbear
  • 13,753
  • 20
  • 44
  • 64
77
votes
7 answers

Why does the greedy coin change algorithm not work for some coin sets?

I understand how the greedy algorithm for the coin change problem (pay a specific amount with the minimal possible number of coins) works - it always selects the coin with the largest denomination not exceeding the remaining sum - and that it always…
user1311286
15
votes
3 answers

SICP example: Counting change, cannot understand

I am a beginner following SICP course on MIT OpenCourseWare using both the video lectures and the book available online. Yesterday I came across an example, which ask if we can write a procedure to compute the number of ways to change any given…
nonameable
  • 328
  • 2
  • 11
13
votes
5 answers

SICP making change

So; I'm a hobbyist who's trying to work through SICP (it's free!) and there is an example procedure in the first chapter that is meant to count the possible ways to make change with american coins; (change-maker 100) => 292. It's implemented…
RyanD
  • 163
  • 1
  • 7
9
votes
2 answers

Dynamic Programming Coin Change Problems

I am having issues with understanding dynamic programming solutions to various problems, specifically the coin change problem: "Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm}…
zc917
  • 97
  • 1
  • 4
8
votes
9 answers

Coin change DP solution to keep track of coins

Trying to program a DP solution for the general coin-change problem that also keeps track of which coins are used. So far I have it working to give me the minimum amount of coins needed but can't figure out how to get which coins were used and how…
Javier Garrido
  • 81
  • 1
  • 1
  • 2
6
votes
5 answers

Finding shortest combinations in array/sequence that equals sum

I'm totally stuck and have no idea how to go about solving this. Let's say I've an array arr = [1, 4, 5, 10] and a number n = 8 I need shortest sequence from within arr which equals n. So for example following sequences within arr equals n c1 =…
user151193
  • 212
  • 2
  • 9
6
votes
4 answers

Number of ways to change coins in constant time?

Let's say I have three types of coins -- a penny (0.01), a nickel (0.05), and a dime (0.10) and I want to find the number of ways to make change of a certain amount. For example to change 27 cents: change(amount=27, coins=[1,5,10]) One of the more…
David542
  • 96,524
  • 132
  • 375
  • 637
6
votes
3 answers

Given an elevator with max weight and n people with x_i weights, find out minimum number of rides needed

input: max_weight = 550 n = 4 x_i = [120, 175, 250, 150] output: 2 // [[250, 175, 120], [150]] My initial impression is that this looks very similar to a dynamic programming coin change/knapsack problem, however it is not coin change (which…
6
votes
4 answers

Dynamic Programming Coin Change Limited Coins

Dynamic Programming Change Problem (Limited Coins). I'm trying to create a program that takes as INPUT: int coinValues[]; //e.g [coin1,coin2,coin3] int coinLimit[]; //e.g [2 coin1 available,1 coin2 available,...] int amount; //the amount we want…
DIMITRIOS
  • 195
  • 1
  • 9
6
votes
3 answers

Recursive Algorithm Time Complexity: Coin Change

I'm going through some algorithms, and came across the coin change problem. When thinking about the problem I came up with this naive recursive solution: int coinChange(const vector& coins, int start, int n) { if (n == 0) return 1; if (n <…
Dominic Farolino
  • 1,217
  • 1
  • 16
  • 37
6
votes
3 answers

Recursive change-making algorithm

Given a target amount and a list of coin denominations, my code is supposed to find the fewest coins needed to reach the target amount. Examples: C(78, [1, 5, 10, 25, 50]) = 6 we can make 78 from 3x25 + 3x1, so 6 coins are required C(48, [1, 7,…
user1681664
  • 1,533
  • 8
  • 23
  • 50
5
votes
3 answers

Thought process for arriving at dynamic programming solution of Coins change problem

I am learning dynamic programming and came across this famous coins change problem. The reccurence relation to solve this problem is given by countCoinsChangeRec(arr, sum - arr[i], i) + countCoinsChangeRec(arr, sum, i - 1); The simplest way to…
Arun Gowda
  • 1,715
  • 4
  • 17
  • 34
5
votes
2 answers

Coin change with limited number of coins

I have written a program for generating subset sum which might be used in this problem which states: Suppose, you have 3 $1-coins, 2 $2-coins, 3 $5-coins, 1 $10-coin, there are 4 ways to obtain $10 from those coins. If there are n1 $X1 …
5
votes
1 answer

Algorithms to compute Frobenius Numbers of a set of positive integers

Frobenius numbers of a set exist iff the gcd of the numbers of the set is 1. Given a set of positive integers with at most 10 elements such that the gcd of all the elements is 1, how can we compute the Frobenius number of the set? Here is the link…
1
2 3
14 15