Search code examples
dynamic-programmingcoin-change

Find coins which sum to target amount with a restricted set


Given a sum amount 1.15 Rs. (1 Rs. = 100 Paise), so total 115 paise and given a list of 8 coins with denominations as {1, 2, 5, 10, 20, 25, 50, 100} paise. Find 6 coins which sum to 1.15 Rs. The constraint is that I should not be able to give change from my solution for the amounts given in a restricted set. The restricted set here is {5, 10, 20, 25}.

Appreciate any solutions or pointers.


Solution

  • Is this what you are looking for?

    import java.util.Arrays;
    public class Coining {
    
    public static void getChange(int amount, int[] denomination){
        Arrays.sort(denomination);//sort the array
        for(int coin=denomination.length-1; coin>=0;coin--){
            int coef = amount/denomination[coin];
            amount%=denomination[coin];
            if(coef > 0)
                System.out.format("%d {%d Paise}%n",coef, denomination[coin]);
            if(amount == 0)
                return;
        }
    }//
    
    public static void main(String... args){
        //int coins[]={1,2,5,10,20,25,50,100}; THIS IS IRRELEVANT.
        int restricted[]={5,10,20,25};
        int amount = 115;
        getChange(amount,restricted);
    }//
    }