The coin change problem is very well documented. Given an infinite supply of coins of denominations x_1
to x_m
you need to find the number of combinations which add up to y
. For example, given x = {1,2,3}
and y = 4
we have four combinations:
{1,1,1,1}
{1,1,2}
{1,3}
{2,2}
Introduction
There are several variations of the coin change problem. In this variation we have two additional restrictions:
- Every denomination must be used at least once.
- Exactly a fixed number of coins must be used in total.
For example, given x = {1,2,3}
, y = 36
and n = 15
where n
is the total number of coins that must be used, we get four combinations:
{1,2,2,2,2,2,2,2,3,3,3,3,3,3,3}
(1 ones, 7 twos, 7 threes){1,1,2,2,2,2,2,3,3,3,3,3,3,3,3}
(2 ones, 5 twos, 8 threes){1,1,1,2,2,2,3,3,3,3,3,3,3,3,3}
(3 ones, 3 twos, 9 threes){1,1,1,1,2,3,3,3,3,3,3,3,3,3,3}
(4 ones, 1 twos, 10 threes)
Challenge
The challenge is to write a function enumerate
in the language of your choice which enumerates all the combinations as described above given:
- The list of denominations. For example
{1,5,10,25}
. You may use either lists or arrays. - A non-negative integer
y
that denotes the sum of every combination. - A non-negative integer
n
that denotes the total number of coins.
The order of the arguments doesn't matter. Pointfree functions are allowed.
The output of the enumerate
function must be a list of combinations. Each combination must be unique and it must be a list of n
integers which add up to y
. Every denomination must appear at least once in each combination and no combination must be missing. The ordering of the integers and the combinations doesn't matter. You may use either lists or arrays for the output.
Keep in mind the following edge cases:
- If both
y
andn
are zero and the list of denominations is empty then the output is a list of one combination, the empty combination (i.e.{{}}
). - Otherwise, if
y
is zero,n
is zero or the list of denominations is empty then the output is a list of zero combinations (i.e.{}
). - More generally, if
y
is less than the sum of the denominations orn
is less than the number of denominations then the output is a list of zero combinations.
Scoring will be based on the size of the entire program in bytes. Note that this includes the enumerate
function, helper functions, import statements, etc. It does not include test cases.
y
is less than the sum of the denominations then at some point in your recursive solution you'll reach the base case where the list of denominations is empty. Hence, the answer will be{}
(i.e. no solution found). Ifn
is less than the number of denominations then you'll eventually reach the base case wheren = 0
buty != 0
. Hence, the answer will again be{}
. \$\endgroup\$ – Aadit M Shah Jul 29 '16 at 10:37