-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindiaCoins.java
More file actions
54 lines (43 loc) · 1.66 KB
/
Copy pathindiaCoins.java
File metadata and controls
54 lines (43 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// import java.util.Arrays;
// import java.util.Comparator;
// import java.util.ArrayList;
public class indiaCoins {
public static void main(String[] args) {
Integer coins[] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000};
int amount = 220;
int[] result = new int[coins.length];
int remainingAmount = amount;
for (int i = coins.length - 1; i >= 0; i--) {
if (remainingAmount >= coins[i]) {
result[i] = remainingAmount / coins[i];
remainingAmount %= coins[i];
}
}
System.out.println("Coins used to make the amount " + amount + ":");
for (int i = 0; i < coins.length; i++) {
if (result[i] > 0) {
System.out.println(coins[i] + " x " + result[i]);
}
}
//2nd approach
// Integer coins[] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000};
// Arrays.sort(coins, Comparator.reverseOrder());
// int countOfCoins = 0;
// int amount = 590; // Example amount to make change for
// ArrayList<Integer> ans = new ArrayList<>();
// for(int i=0; i < coins.length; i++) {
// if (coins[i] <= amount) {
// while(coins[i] <= amount) {
// countOfCoins++;
// ans.add(coins[i]);
// amount -= coins[i];
// }
// }
// }
// System.out.println("total (min) coins used = " + countOfCoins);
// for(int i=0; i < ans.size(); i++) {
// System.out.print(ans.get(i) + " ");
// }
// System.out.println();
}
}