-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha56indianCoins.java
More file actions
27 lines (25 loc) · 856 Bytes
/
Copy patha56indianCoins.java
File metadata and controls
27 lines (25 loc) · 856 Bytes
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
public class a56indianCoins {
public static void main(String[] args) {
Integer coins[] = {1,2,5,10,20,50,100,500,2000};
int amount = 5590;
Arrays.sort(coins, Comparator.reverseOrder());
ArrayList<Integer> ans = new ArrayList<>();
int countOfCoins = 0;
for(int i=0; i<coins.length; i++){
if (coins[i]<=amount) {
while (coins[i]<=amount) {
countOfCoins++;
amount -= coins[i];
ans.add(coins[i]);
}
}
}
System.out.println("Total coins used : "+countOfCoins);
for(int i=0; i<ans.size(); i++){
System.out.print(ans.get(i)+" ");
}
}
}