Sherlock and The Beast

Sort by

recency

|

542 Discussions

|

  • + 0 comments

    C++

    void decentNumber(int n) {
        for (auto i = 0; i <= n; ++i) {
            if (i % 5 == 0 && (n - i) % 3 == 0) {
                cout << string(n - i, '5') << string(i, '3') << endl;
                return;
            }
        }
        cout << -1 << endl;
    }
    
  • + 0 comments

    A few problems with this task 1) For n=15 both could work all 5 or all 3 but test just expect it to be all 5 2) 33333555 and 55533333 are valid solutions but again test expect it to be 55533333 3) for n = 100 there more then one solution it could be 10 times 5 and 90 times 3 but also could be 85 times 3 and 15 times 5

    All in all i've decided to not proceed because it doesn't make sense find all of the limitations that not descripbed in the Problem section.

    def decentNumber(n):
        for i in range(1,n//3+1):
            if (n - i*5) % 3 == 0:
                n5 = n - i*5
                n3 = n - n5
                return '5'*n5 + '3'*n3     
                
            if (n - i*3) % 5 == 0:
                n3 = n - i*3
                n5 = n - n3  
                # print('n3-n5', n3,n5)
                return '5'*n5 + '3'*n3
            
        return -1
    ``
    

    `

  • + 0 comments

    python 3

    def decentNumber(n):
        fives = n
        threes = 0
        
        while fives >= 0:
            if fives % 3 == 0 and threes % 5 == 0:
                break
            else:
                fives -= 1
                threes += 1
        print(-1 if fives < 0 else ('5' * fives) + ('3' * threes))
    
  • + 0 comments

    Here is my c++ solution, you can have the implementation link here : https://youtu.be/R01NQ4Zt2qA

    void decentNumber(int n) {
        int three = ((3 - (n % 3)) % 3 ) * 5;
        if(three > n) cout << -1;
        else cout << string(n - three, '5') << string(three, '3');
        cout << endl;
    }
    
  • + 0 comments

    My Java solution with o(n) time complexity and o(1) space complexity:

    public static void decentNumber(int n) {
            int fives = n;
            while (fives % 3 != 0) {
                fives -= 5;
            }
            if (fives < 0) {
                System.out.println("-1");
            } else {
                System.out.println("5".repeat(fives) + "3".repeat(n - fives));
            }
        }