MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / CoinChangeCombination

Class CoinChangeCombination

Programs/CombinationCoinChange.java:5–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3import java.util.List;
4/*Print all the Combinations of Change for a given Amount*/
5 class CoinChangeCombination {
6
7 private static List<Integer> coins;
8 private static int count=0;
9
10 private static void init() {
11 coins = new ArrayList<Integer>();
12 coins.add(1);
13 coins.add(3);
14 coins.add(5);
15 coins.add(9);
16 coins.add(10);
17 coins.add(14);
18 coins.add(18);
19 coins.add(23);
20 }
21
22 /*Prints all comninations of the coin change*/
23 public static void coinCombinations(int amount,int index,LinkedList<Integer> list)
24 {
25 if(amount==0)
26 {
27 count++;
28 System.out.println(list);
29 return ;
30 }
31
32 if(amount < 0)
33 return ;
34
35 for(int i=index ; i < coins.size();i++)
36 {
37 int coin = coins.get(i);
38 if(amount >= coin)
39 {
40 list.add(coin);
41 coinCombinations(amount - coin ,i,list );
42 list.removeLast();
43
44 }
45 }
46 }
47
48 public static void main(String[] args) {
49 int amount = 175;
50 init();
51 coinCombinations(amount,0,new LinkedList<Integer>());
52 //System.out.println("Number of Combinations :" + count);
53 }
54}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected