2

I am trying to find the large number in java by using the integer parameter. I converted the integer value to string but the problem is, it failed on the run and did not show the expected result.

Here is the case:

Given an integer n, it returns the largest number that contains exactly n digits only when n is non-zero.

  • For n = 1, the output should be largestNumber(n) = "9".
  • For n = 2, the output should be largestNumber(n) = "99".
  • For n = 3, the output should be largestNumber(n) = "999".
  • For n =0, the output should be largestNumber(n) = "-1".

Constraints 0 ≤ |n| ≤ 10

Here is the java code :

class Result {
  /*
   * Complete the 'largestNumber' function below.
   *
   * The function is expected to return a STRING.
   * The function accepts INTEGER n as parameter.
   */
public static String largestNumber(int n) { // Write your code here
String res = Integer.toString(n);
return res;
}
                   
}public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim()); String result = Result.largestNumber(n);
        bufferedWriter.write(result);
        bufferedWriter.newLine();
        bufferedReader.close();
        bufferedWriter.close();
    }
}

Here is the result of the screenshot.

Result

2
  • Why should n = 0 result in largestNumber(n) = "-1"? It should be 0 since all the other values are = 10^n - 1. So for 0, 10^0 - 1 == 0. To say it should be -1 is an anomaly in the specification and the requestor should be challenged. Commented Oct 26, 2021 at 22:02
  • @k314159 That formula just happens to work for positive n, but within the context of the problem, 0 has one digit, not zero digits. -1 is used to indicate that this is a special case with no clear solution. Commented Oct 26, 2021 at 22:05

2 Answers 2

2

All you have to do is repeat the digit "9" n times, with the exception of returning -1 for 0.

public static String largestNumber(int n) {
     if(n == 0) return "-1";
     // for Java 11+: return "9".repeat(n);
     StringBuilder sb = new StringBuilder(n);
     for(int i = 0; i < n; i++) sb.append('9');
     return sb.toString();
}
Sign up to request clarification or add additional context in comments.

3 Comments

In Java 11 or higher, you can repeat a string simply with "9".repeat( n )
I'd like to offer an alternative: String.format("%d",(int)Math.pow(10, n)-1)
@GonenI using String.format for trivial conversions like String.format("%d",…) is not better than "" + … or String.valueOf(…). When you look at its documentation and follow the format string) link and consider the underlying framework’s features (locale sensitive formatting, layout justification and alignment), it’s not surprising that it will consume far more resources than string concatenation.
1
public static String largestNumber(int n) {
    if (n == 0)
        return "-1";
    return BigInteger.TEN.pow(n).subtract(BigInteger.ONE).toString();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.