Sherlock and The Beast

Sort by

recency

|

537 Discussions

|

  • + 0 comments

    python3

    def decentNumber(n):
        # Write your code here
        
        t = n - n % 3
        found = False
        
        while t >= 0 and not found:
            if (n - t) % 5 == 0:
                found = True
            else:
                t = t - 3
    
        print("5" * t + "3" * (n-t)) if found else print(-1)
    
  • + 0 comments

    Java:

    public static void decentNumber(int n) {
      // Start by finding the largest multiple of 3 less than or equal to n
      for (int i = n - (n % 3); i >= 0; i -= 3) {
        int remaining = n - i;
    
        // Check if the remaining is a multiple of 5
        if (remaining % 5 == 0) {
          // Append '5' i times
          StringBuilder result = new StringBuilder();
          result.append("5".repeat(i));
    
          // Append '3' (n - i) times
          result.append("3".repeat(remaining));
    
          System.out.println(result);
          return; // Exit as we've found the solution
        }
      }
    
      // If no valid number is found, print -1
      System.out.println(-1);
    }
    
  • + 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
    def decentNumber(n):
        for i in range(n + 1):
            if (n - i) % 3 == 0 and i % 5 == 0:
                print((n - i)*'5' + (i)*'3')
                return
        print(-1)
    
  • + 0 comments

    python

    def decomposeNumberBy3and5(n):
        """If possible, get {3: a, 5: b}, such that n = a*3 + b*5.
        3 has the priority."""
        if n in (1, 2, 4, 7):
            return None
    
        match n % 3:
            case 0:
                return {3: n // 3, 5: 0}
            case 1:
                return {3: n // 3 - 3, 5: 2}  # 1 = 5 * 2 - 3 * 3
            case 2:
                return {3: n // 3 - 1, 5: 1}  # 2 = 5 * 1 - 3 * 1
    
    
    def decentNumber(n):
        if (decomp := decomposeNumberBy3and5(n)) is None:
            print(-1)
        else:
            print("5" * decomp[3]*3 + "3" * decomp[5]*5)