So I have the coin change problem figured out and I understand how it works and everything but I cant seem to figure out how to print out how many of each coin is used. For example with the amount of 12 and the coin array of 1, 5, and 10 I want the output to look like:
Penny. Nickel. Dime
12. 0. 0
7. 1. 0
2. 2. 0
2. 0. 1
How would I go about printing that out? The code I currently have is:
public class codingChallenge {
public static void main(String[] args) {
int [] coinsArray = {1, 5, 10};
System.out.println(change(12, coinsArray));
}
public static int change(int amount, int[] coins){
int[] combinations = new int[amount + 1];
combinations[0] = 1;
for(int coin : coins){
for(int i = 1; i < combinations.length; i++){
if(i >= coin){
combinations[i] += combinations[i - coin];
System.out.println(coin);
}
}
System.out.println();
}
return combinations[amount];
}
}
Any help is VERY appreciated. Thanks!