This question occurred as part of an increasingly-difficult problem in an interview. It started ever so simply:
(1) Assuming an infinite supply of coins (in the usual 1, 5, 10, 25 cent denominations). Given n cents, is there always a way to make change for it using the normal denominations?
Yes, since the penny divides all possible values of n cents.
(2) Good, now write a program that accepts n (positive) cents, and returns one possible way of making change for it
Return n pennies.
(3) Smart ass. What if you want to minimize the number of coins required to make the change?
Start with the largest denomination d_i, and take the maximum number of them such that you don't exceed n, m_i. Take n - (d_i)(m_i) and repeat for next largest denomination.
(4) Good, can you prove this solution is optimal?
Yes, { blah, blah }
(5) Ok, *smirk* , now what if, in addition to the n cents, you were given an arbitrary-sized array consisting of arbitrary denominations? You can assume each denomination occurs only once in the array, and that all denominations are positive
My initial thought was just to sort the array of denominations, and apply the same logic as in (4). Luckily, before I communicated this, I caught myself and realized it wouldn't work. But now I realized I was in a pickle.
My next thought was to apply the sum-subset problem to each divisor of n, but realized this was probably overkill. The solution I ended up providing just used the Change-making problem, and short-circuited it when I found some solution. I feel like there has to be a smarter way of doing this though..
The problem reduces to: Given a finite set S of distinct natural numbers, find a linear combination of elements of S that (1) sum to another natural number n, (2) minimize the sum of coefficients in the lin.combination
All you’re doing is determining all of the ways you can come up with the denomination of 8 cents. Eight 1 cents added together is equal to 8 cents. Three 1 cent plus One 5 cents added is 8 cents.
Likewise to up to m coin, If we select mth coin in the start (value = C[m-1]), Now smaller problem is minimum number of coins required to make change of amount (A - C[m-1]) i.e. minCoin(A - C[m-1]).
Three 1 cent plus One 5 cents added is 8 cents. So there are a total of 2 ways given the list of coins 1, 5 and 10 to obtain 8 cents. Example 2: Suppose you are given the coins 1 cent, 5 cents, and 10 cents with N = 10 cents, what are the total number of combinations of the coins you can arrange to obtain 10 cents.
The two often are always paired together because the coin change problem encompass the concepts of dynamic programming. For those who don’t know about dynamic programming it is according to Wikipedia,
Actually this problem has been studied as Canonical coin systems, and we even got a paper about how to determine whether a given coin system can support a greedy solution. The original paper may give you some insights: Canonical coin systems for change-making problems.
Alternatively, you can google the key word "Canonical coin systems" for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With