Amy is a cashier at a convenience store and has run out of all dollar bills. But she has plenty of coins--pennies (1 cent), nickels (5 cents), dimes (10 cents), quarters (25 cents)--in the cash drawer, and is well trained for an emergency situation like this: give the change with the fewest possible coins!
She just gave a change with 20 coins. What is the minimum possible value of the change, in cents?
Clarification: For example, if you think the change she just gave her customer is 2 dollars and 37 cents, write 237.
Bonus Question: What's the general formula when the number of coins is not 20 but some integer n ≥ 6 ?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Is the way that the question is worded not wrong? "She just gave a change with 20 coins. What is the minimum possible value of the change, in cents?". If we are looking for the minimum value, why isn't the answer 20 pennies = 20 cent??
Log in to reply
The key is the bolded phrase: she will ONLY give change using the fewest possible coins. If the change is 20 cents, she would just use 2 dimes instead of 20 pennies because it takes less coins.
Log in to reply
but the question says that she gave a change of 20 COINS, and the lowest possible value of 20 COINS is 20 PENNIES = 20 cents.
Why can't the 20 coins be 4x1 + 1x5 + 2x10 + 13x25 = $3.54, less than your $3.69.
Log in to reply
You can’t have two dimes AND a nickel.
$3.54 can be achieved with 4 pennies and 14 quarters, which is only 18 coins.
There are other solutions. $3.74 is 14q+2d+4p.
Log in to reply
True, but the question asked for the minimum amount of change, so there is only one solution.
That made me understand a lot more
I honestly think this problem is poorly worded/structured. The first paragraph poses the idea: what's the least amount of coins that can be used to express a given amount of money.
But then it asks you to answer: "what is the lowest monetary value that can be expressed with 20 coins"
Log in to reply
I agree that the problem could have been worded more clearly. A better question would be, "If a certain amount of change forces Amy to use at least 20 coins, what is the minimum possible value of this change, in cents?" It just needs to be clear that there is no better way than 20 coins. However, I still believe what the writer of the question was trying to ask is clear based on the first paragraph.
Log in to reply
I still think it's not. You can either minimize the coin total based on a given a set monetary value, or viceversa. You answer doesn't really answer either. $3.69 is not the minimum amount of money that can be represented with 20 coins.
Wait, but how would Amy give change for 20 cents?
Log in to reply
Assuming that 20 is the correct answer is a common mistake people are making. The reason this is not correct is that Amy would actually use 2 dimes instead of 20 pennies to pay that amount.
There is no requirement to use all the coins, thus only the total number of coins is necessary (i.e. 20). Hence $4.76 (19 quarters +1 penny) should also be a valid answer; no faster way exists to achieve this solution as no one-dollar bills exist.
Log in to reply
The question asks for the minimal possible cost of change. $4.76 is not the cheapest price for which 20 coins are necessary, so it is incorrect. The reason I decide to not just use quarters is because to minimize cost, the goal is to use as many pennies and nickels as possible and less dimes or quarters.
Let p , n , d , q be the numbers of pennies, nickels, dimes, and quarters; and N = p + n + d + q the total number of coins.
As long as p ≥ 5 , we can replace five pennies by one nickel, reducing N by four. Therefore in a minimum number of coins situation, p ≤ 4 . As long as n ≥ 2 , we can replace two nickels by one dime, reducing N by one. Thus n ≤ 1 . As long as d ≥ 3 , we can replace three dimes by a quarter and a nickel, reducing N by one. Thus d ≤ 2 . Finally, if after all this n + d = 3 (that is, n = 1 and d = 2 ), two dimes and a nickel may be exchanged for a quarter, reducing N by two. Therefore n + d ≤ 2 .
Within these constraints we want to maximize p , then n , then d . Thus we choose p = 4 , n = 1 , d = 1 , q = N − 6 , and the amount in cents is 4 ⋅ 1 + 1 ⋅ 5 + 1 ⋅ 1 0 + ( N − 6 ) ⋅ 2 5 = 2 5 ( N − 6 ) + 1 9 . In the case of N = 2 0 this results in 2 5 ⋅ 1 4 + 1 9 = 3 6 9 .
OK, but also: 4+ 5+10+25 =44 << 7 COINS (4+ 5+10+25) X2 =88 << 14 COINS (4+ 5+10+25) X3 =132 << 21 COINS ...x4 (16+20+40+100) =176 << 28 COINS ( 4+ 20+40+100) =176-1 12=164 << 16 COINS ( 4+ 40+40+100) =164+5 4 << 20 COINS ( 4+ 5+40+100) =164-5 7 << 13 COINS ( 4+ 5+110+100) =149+10 7 << 20 COINS ( 4+ 5+ 10+100) =219-10 10 << 10 COINS ( 4+ 5+ 10+350) =219+25 10 << 20 COINS =369 OR the les 20 pennies for sommé monkeys l'ke me ;)
Log in to reply
sorry but after thinking, 369 -25 +10 = 354 is the best no? 1x4 + 5x1 + 10x2 + 25x13 Right?
Log in to reply
No. Your 10x2 + 5x1 can be replaced with 25x1.
Log in to reply
@Mark Chipman – i saw it also... but i'm a Rebel ;)
And sorry for the Littré mistake here
>>( 4+ 5+ 10+350) =119+2510 << 20 COINS =369
What about the following : 4 cents, 1 nickel, 4 dimes, 11 quarters ? This makes 20 coins. The amount is : 4 1 + 1 5 + 4 10 + 11 25 = 4 + 5 + 40 + 275 = 324 This is clearly better than the proposed solution ???
Log in to reply
No, because in order to pay $3.24 she would instead give 4 cents, 2 dimes and 12 quarters; a total of 18 coins.
It should be clear that the goal is to have the most number of small coins as possible.
4 pennies is the most amount of pennies you can have, since 5 pennies would turn into 1 nickel. 1 nickel is the most amount of nickels you can have, since 2 nickels is a dime. 2 dimes is the most amount of dimes you can have. Having more than two will either have a same number or less coins when replacing dimes and/or nickels with quarters. You cannot simultaneously have two dimes and one nickle, otherwise you'll have a quarter.
So you can have 4 pennies, 1 nickel, 1 dime. All other coins must be quarters.
If there are 20 coins, then 14 must be quarters, 1 nickel, 1 dime, and 4 pennies. This leaves $3.69 as the minimum that can be provided with 20 coins.
It's also clear from this that for any number of coins greater than six, you must only use quarters. So the general formula for N coins is (N - 6) quarters, 4 pennies, 1 nickel, and 1 dime.
I'm getting the answer as 354 (Value of coin) * ( number of coins)= (answer) 20x13= 325, 10x2= 20, 5x1=5, 1x4=4, 325+20+5+4= 354 Most of the solutions have only one 10 cent coins, what if I have to pay 20 cents? I can have two 10 cent coins. Correct me if I've made an obvious error that I missed.
Log in to reply
I thought the same, but 10, 10, 5 can be replaced by a 25cents coin.
Log in to reply
Im agree with that but, for me, 354 seem the more suitable and plaisant soluce, isn't it? ;)
A quarter is 25 cents, not 20 cents.
python bruteforce solution :)
although it is clear that we need to maximize the no. small value coins up to the point before they reach the next coin class to have the minimum value of coins overall
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Log in to reply
Or This number coins = 20 coin v = [25,10,5,1] coin c = [number coins,-1,-1,-1]
coin c[1] += coin v[0]//coin v[1] coin c[0] -= coin c[1] coin c[2] += coin v[1]//coin v[2] coin c[0] -= coin c[2] coin c[3] += coin v[2]//coin v[3] coin c[0] -= coin_c[3]
csum=0 print ( list( zip(coin c,coin v) ) )
;)
Log in to reply
sorry for french Word auto transate... and for Bad tabulation... Im newbie !
This code works for any denominations
What if you had 4 pennies, no nickels, 4 dimes, and 1 2 quarters? It never said you couldn’t have no nickels. This would make 4 + 4 0 + 3 0 0 = 3 4 4 . This is better than 3 6 9 .
well, in this case we have plenty of all, but it does make for an interesting problem with other restraints
You can use 0 of a coin type, but "40" can be obtained not only by 10+10+10+10 but also by 25+5+10 - so that doesn't fit the requirements (344 is can be obtained with 19 coins then)
Just for fun! This works for any denominations and any n. The for loop can be simplified but I have a weak spot for zip 😇
1 2 3 4 5 6 7 8 9 10 |
|
A program in python 3 to solve this as follows:
valQuarters = 25
valDimes = 10
valNickels = 5
valPennies = 1
numCoins = 20
change=100 # start with a minimal initial guess
notFinish=True
while(notFinish):
q = change // valQuarters
d = (change - valQuarters * q) // valDimes
n = (change - valQuarters * q - valDimes * d) // valNickels
p = (change - valQuarters * q - valDimes * d - valNickels * n)
change+=1
if q + d + n + p == numCoins:
print("q =",q,"d =",d,"n =",n,"p =",p)
print("minimum possible value =",q * valQuarters + d * valDimes + n * valNickels + p * valPennies )
notFinish=False
God want FBI killer
A bashy approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Python 3 solution below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
Let x 0 , x 1 , x 2 , and x 3 be the number of coins for 2 5 cents, 1 0 cents, 5 cents and 1 cent respectively. First, I initialize that the value is equal to the total number of coins. So, in this case, the inilization value is 2 0 . Define v 0 = 2 0 , and
If i = 0 ∑ 3 x i = 2 0 , then add one to v 0 and repeat from the 1 s t step. Otherwise, that is i = 0 ∑ 3 x i = 2 0 , we get v 0 which is the minimum value of 2 0 coins.
Here is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
for num in range(1000): n = num x = 0 while n >= 25: n = n - 25 x = x + 1 else: while n >= 10: n = n - 10 x = x + 1 else: while n >= 5: n = n - 5 x = x + 1 else: while n >= 1: n = n - 1 x = x + 1 else: if x == 20: print (num)
So if anyone would like to use code to solve this problem, I found this page on how to use dynamic programming to find the minimum coins needed for a given amount of change on interactivepython.org, which helped me find the minimum number of coins for a given amount of change. I simply added a while loop to get the first instance of change requiring a minimum of 20 coins.
If you're at all interested in learning about dynamic programming, I'd recommend checking that page out.
I realize now that this would've been much easier to solve by thinking about it rather than coding, but finding a code solution was still fun.
Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
WHAT ABOUT THIS CODE: coin v = [25,10,5,1] coin c = [20,-1,-1,-1] soluce = [13,2,1,4]
coin c[1] += coin v[0]//coin v[1] coin c[0] -= coin c[1] coin c[2] += coin v[1]//coin v[2] coin c[0] -= coin c[2] coin c[3] += coin v[2]//coin v[3] coin c[0] -= coin_c[3]
csum=0 print ( list( zip(coin c,coin v) ) )
If number is less 25, we can use no more than 6 coins for 19 and 24. If more than 25, use 25 cent coins until it is less. So 14 25-cents and 19 cents with 10,5,1,1,1,1.
I was under the assumption that you wanted to write a program to find the solution to the above problem, so I wrote a program in Free Pascal since it's what I had available.
I wrote a program for the general case. I.E; I wrote a program given n denominations of coins c 1 , c 2 , . . . , c n each having a value v n . The program outputs the minimum possible value of the change in cents.
program demonstration;
type arraytype = array[0 .. 100] of longint;
var coin,value,m:arraytype;
numcoins,total:longint;
procedure getinput; {general case}
var j:longint;
begin
writeln('Enter number of denominations');
readln(numcoins);
writeln('Enter total number of coins to be given as change');
readln(total);
writeln('Enter the value of each coin in decreasing order');
for j:= 1 to numcoins do
begin
read(value[j]);
end;
end;
procedure switch(var x,y:longint);
var temp:longint;
begin
temp:= x;
x:= y;
y:= temp;
end;
procedure arrange;
{In case input was not entered in decreasing order.}
var j,k:longint;
begin
for j:= 1 to (numcoins - 1) do
begin
for k:= j to numcoins do
begin
if value[j] < value[k] then
switch(value[j],value[k]);
end;
end;
end;
procedure general_amount;
var s,sum,j:longint;
begin
s:= 0;
repeat
s:= s + 1;
m[0]:= s;
for j:= 1 to numcoins do
begin
coin[j]:= m[j - 1] div value[j];
m[j]:= m[j - 1] - value[j] * coin[j];
end;
sum:= 0;
for j:= 1 to numcoins do
begin
sum:= sum + coin[j];
end;
until(sum = total);
writeln(s);
end;
begin
getinput;
writeln;
arrange;
general_amount;
readln;
end.
Running the above program we obtain 3 6 9 .
python is free too.
We only need to analyze the 25 cases in the table--from 15 cents to 39 cents--because the next 25 cases (40~64 cents) are each only 1 quarter more than what's in the table, the next 25 cases (65~89 cents) are each 2 quarters more, and so on.
In the "Total # of coins" column of the table, we see a clear pattern: 2 3 4 5 6 2 3 4 5 6 1 2 3 4 5 2 3 4 5 6 2 3 4 5 6 , where the hightest number 6 appears four times and the first 6 corresponds to a change of 19 cents. Also, note that the smallest number, 1, ( which is 5 less than the greatest number ) corresponds to a change of 25 cents ( which is 6 cents more than the 19 cents ) . Therefore, as indicated above, the 1 4 th from this pattern would be a pattern with only 14 more on each number: 1 6 1 7 1 8 1 9 2 0 1 6 1 7 1 8 1 9 2 0 1 5 1 6 1 7 1 8 1 9 1 6 1 7 1 8 1 9 2 0 1 6 1 7 1 8 1 9 2 0 . Since the smallest number, 15, corresponds to 1 5 × 2 5 = 3 7 5 cents, the smallest change corresponding to 20 coins is 3 7 5 − 6 = 3 6 9 cents.
In general, given n ( ≥ 6 ) coins, the formula for the minimum possible value of the change in cents is ( n − 5 ) × 2 5 − 6 . □
Cents | # of pennies(1) | # of nickels(5) | # of dimes(10) | # of quarters(25) | Total # of coins |
15 | 0 | 1 | 1 | 0 | 2 |
16 | 1 | 1 | 1 | 0 | 3 |
17 | 2 | 1 | 1 | 0 | 4 |
18 | 3 | 1 | 1 | 0 | 5 |
19 | 4 | 1 | 1 | 0 | 6 |
20 | 0 | 0 | 2 | 0 | 2 |
21 | 1 | 0 | 2 | 0 | 3 |
22 | 2 | 0 | 2 | 0 | 4 |
23 | 3 | 0 | 2 | 0 | 5 |
24 | 4 | 0 | 2 | 0 | 6 |
25 | 0 | 0 | 0 | 1 | 1 |
26 | 1 | 0 | 0 | 1 | 2 |
27 | 2 | 0 | 0 | 1 | 3 |
28 | 3 | 0 | 0 | 1 | 4 |
29 | 4 | 0 | 0 | 1 | 5 |
30 | 0 | 1 | 0 | 1 | 2 |
31 | 1 | 1 | 0 | 1 | 3 |
32 | 2 | 1 | 0 | 1 | 4 |
33 | 3 | 1 | 0 | 1 | 5 |
34 | 4 | 1 | 0 | 1 | 6 |
35 | 0 | 0 | 1 | 1 | 2 |
36 | 1 | 0 | 1 | 1 | 3 |
37 | 2 | 0 | 1 | 1 | 4 |
38 | 3 | 0 | 1 | 1 | 5 |
39 | 4 | 0 | 1 | 1 | 6 |
Problem Loading...
Note Loading...
Set Loading...
To minimize the amount of change, consider maximizing low-costing coins first. If the number of pennies is greater than or equal to 5, 5 of them can be replaced with a nickel. Therefore, the maximum number of pennies can be 4.
Now the coins to consider are nickels, dimes, and quarters. If the number of nickels is greater than or equal to 2, 2 of them can be replaced with a dime. Therefore, the maximum number of nickels can be 1.
When considering dimes, we need to be a bit more careful. It seems that it is possible to have 2 dimes, but if there is 1 nickel, then these can be replaced with a quarter. Without the nickel, the maximum number of dimes is 2 (3 dimes can be replaced with a nickel and a quarter). The first option minimizes the cost: 1 nickel and 1 dime are better than 2 dimes. Clearly, the remaining coins must be quarters.
So the first 6 coins are 4 pennies, a nickel, and a dime, which is 19 cents. The rest are quarters. A general formula for c coins where c ≥ 6 would be ( c − 6 ) ∗ 2 5 + 1 9 . Plugging in 20, we get our answer of 3 6 9 cents or $ 3 . 6 9 .