Possible Duplicate:
Making change recursively: How do I modify my algorithm to print all combinations?
In coin pattern counting problem(Given a value N and a fixed set of coins,we have to calculate the number of combinations of coins that would add up to N.), if we want to print the combinations rather counting the combinations, What is the approach ? Do I have to use dynamic programming for that ?
Yes, you need it. Assuming
dp[i]equals the number of combinations that add up toithen the following pseudocode prints all combinations:This function prints all the combinations of
coins [current_coin .. last_coin]that add up to amount_left. So the initial call would beprint_combinations(N, 0), as you can see the dynamic programming tabledp[]helps us decide if we recurse using the current coin (we only recurse if there is at least one combination adding up to the new amount left which equalsamount_left - coins[current_coin]).