Given a value N, find the number of ways to make change for N cents, if we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins. The order of coins doesn\'t matter
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
total answers (1)
Let's think of the solution. Let’s do it by hand first. An intuitive idea can be two checks whether the amount can be handled by the same valued coins.
Like 4 cent can be managed by four 1 cent coins.
Same time can be managed by two 2 cent coins.
For the rest, we need to take different valued coins.
Now we can think of memorization. We can simply store the sub-amounts that can have been managed still.
Like two 1 cent coin manage 2 cents. Thus 2 cent is our sub amount.
Now we pick two more 1 cent coins that will sum up to 4.
Else we can pick only one 2cent coin also summing up to 4.
This memorized approach helps us to solve the problem.
Let's revise the algorithm
Pre-requisite: Coins array, amount
Algorithm:
Example with explanation:
This actually shows that by coin[1] all the sub-amounts can be managed only by one way, so if we continue this way for other coins we will get all the possible ways.
C++ implementation:
Output