Numbers With Lucky Last Digit

DNS believes that the digits end with 4 and 7 are lucky, and all other digits are unlucky. For example, 4, 14 and 207 are lucky numbers, while 40, 741 and 3 are not lucky numbers. DNS would like to represent the int n as a sum of only lucky numbers, and he would like to do this using the minimal possible number of summands. Return the number of summands in the representation, or -1 if it is impossible to achieve the goal.

input => int n, sum of lucky numbers
output => int n , minimal possible number of summands
Examples
1)i/p: 99 o/p: 4 => 14+24+27+34
2)i/p: 11 o/p: 2
3)i/p: 13 o/p: -1
4)i/p: 1234567 100 o/p: 1
5)i/p: 1 o/p: -1
6)i/p: 2 o/p: -1
7)i/p: 3 o/p: -1
8)i/p: 4 o/p: 1

public class TheNumbersWithLuckyLastDigit {
    public int find(int n) {
        int m=n%10;
        if(m==0) return n>=20?5:-1;
        if(m==1) return n>=11?2:-1;
        if(m==2) return n>=12?3:-1;
        if(m==3) return n>=23?5:-1;
        if(m==4) return n>=4?1:-1;
        if(m==5) return n>=15?3:-1;
        if(m==6) return n>=16?4:-1;
        if(m==7) return n>=7?1:-1;
        if(m==8) return n>=8?2:-1;
        if(m==9) return n>=19?4:-1;
        return -1;
    }
    public static void main(String[] args) {
        TheNumbersWithLuckyLastDigit nw = new TheNumbersWithLuckyLastDigit();
        System.out.println(nw.find(99));
        System.out.println(nw.find(11));
        System.out.println(nw.find(13));
        System.out.println(nw.find(1234567));
        System.out.println(nw.find(1));
        System.out.println(nw.find(2));
        System.out.println(nw.find(3));
        System.out.println(nw.find(4));
        System.out.println(nw.find(5));
        System.out.println(nw.find(6));
        System.out.println(nw.find(7));
        System.out.println(nw.find(8));
        System.out.println(nw.find(9));
        System.out.println(nw.find(10));
        System.out.println(nw.find(11));
        System.out.println(nw.find(12));
        System.out.println(nw.find(13));
        System.out.println(nw.find(14));
        System.out.println(nw.find(15));
    }
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: