-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
MinimumNumOfCoins.java
61 lines (48 loc) · 1.4 KB
/
MinimumNumOfCoins.java
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
55
56
57
58
59
60
61
/*
Minimum number of Coins
----------------------------------------------------------------------------------
Given a value N, if we want to make change for N Rs,
and we have infinite supply of each of the denominations in Indian currency,
i.e., we have infinite supply of { 1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes,
Find the minimum number of coins and/or notes needed to make the change.
Input:
The first line of input contains an integer T denoting the number of test cases.
Each test case consist of an Integer value N denoting the amount to get change for.
Output:
Print all the possible denominations needed to make the change in a separate line.
Constraints:
1 ≤ T ≤ 30
1 ≤ N ≤ 2000
Example:
Input
1
43
Output
20 20 2 1
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int[] array={1, 2, 5, 10, 20, 50, 100, 500, 1000};
for(int i=0;i<t;i++){
int n=sc.nextInt();
MinimumNumberOfCoins(array,n);
System.out.println();
}
}
private static void MinimumNumberOfCoins(int[] array,int n){
int length=array.length;
for(int i=length-1;i>=0;i--){
if(array[i]<=n){
int numOfCoins=n/array[i];
for(int j=0;j<numOfCoins;j++)System.out.print(array[i]+" ");
n%=array[i];
}
}
}
}