You are on page 1of 1

Making change using Greedy Approch :-

import java.util.*;
class HelloWorld {
public static void main(String[] args) {
// int c[]={100,25,10,5,1};
ArrayList<Integer> c = new ArrayList<Integer>();
c.add(100);
c.add(25);
c.add(10);
c.add(5);
c.add(1);
Vector<Integer> S = new Vector<Integer>();
Scanner sc= new Scanner(System.in);
System.out.println("Enter amount want to make change");
int n = sc.nextInt();
int sum=0;
while(sum!=n){
int x= Collections.max(c);
int i;
if((sum+x)<=n){
sum=sum+x;
S.add(x);
}
else{
i=c.indexOf(x);
c.remove(i);
x= Collections.max(c);
}
}
System.out.println(S);
}
}

You might also like