Sherlock and The Beast

Sort by

recency

|

538 Discussions

|

  • + 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

    Here is my Python solution!

    def decentNumber(n):
        nums = list(reversed([i for i in range(n // 3 + 1)]))
        for num in nums:
            if (n - (3 * num)) % 5 == 0:
                print(3 * num * "5" + (n - 3 * num) * "3")
                return
        print(-1)
    
  • + 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
    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)