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.

n = 0result inlargestNumber(n) = "-1"? It should be0since 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.n, but within the context of the problem, 0 has one digit, not zero digits.-1is used to indicate that this is a special case with no clear solution.