Sherlock and The Beast

  • + 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);
    }