package GreedyAlgo; import java.util.ArrayList; public class MinCoinsReq { public static void main(String[] args) { int arr [] = {2,5,10,20,50,100,200,500}; int n =987; int count=0; ArrayList<Integer> a =new ArrayList(); for (int i = arr.length-1; i >0 ; i--) { while (n>=arr[i]) { if (n >= arr[i]) { count++; a.add(arr[i]); n-=arr[i]; } } } System.out.println(count); for(int s:a){ System.out.print(s+" "); } System.out.println(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter