Chewbacca and numbers : what's the error in this code as my 3 out of 5 cases are working as i submit it

import java.util.Scanner;
import java.lang.Math;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
long num = sc.nextLong();
int digits = (int)(Math.log10(num)) + 1;
long [] a = new long[digits];
for(int i = 0 ;i< digits ;i++)
{
a[i] = num%10;
num = num/10;
}
long result = 0;
for(int i =digits-1;i>=0 ; i–)
{
if(a[i]<9-a[i])
{
result = result10 + a[i];
}
else
{
result = result
10 + (9-a[i]);
}
} System.out.println(result);
}
}

Hi
Actually your number in test case is large to be stored in a long number … you must use a string to store the input number and then do the manipulation on string … the given range of input number is 10^100

So I have to go for String array

No a single can store all the digits … like if the number is 10245 then your string can contain β€œ10245” .
1 will be at 0th position ,0 at 1st position and so on for rest of characters…

Then how will I omit the leading zeroes?? (Taking an if statement?)
And also I would be needing to type cast the array of characters into int and then form the number by type casting it and appending it in the string.

Thats is the real deal here if you have 9 as first digit for example in the number 9045 then you cannot convert leading 9 to 0. If there is any leading 9 they will remain 9.And you do not need to type cast just subtract the ascii values … for example if the string is str=5055 then for number at index 0 you can do this int temp=str.charAt(0)-β€˜0’; . This will store the value 5 in temp . Then you can append this to string or directly display it …Do as you please.

I have switched on the collaborate mode ; could you please check which all cases am I not covering??

I used your approach to deal this question .

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String num = sc.nextLine(); StringBuilder res = new StringBuilder(); int count = 0 ; boolean val = true; if(val == true) { for(int i = 0 ; i< num.length() ; i++) { if(num.charAt(i)==β€˜0’) {count++;} else{ break; } }} if(count == num.length()) { System.out.print(β€œ0”); val = false; } if(val == true) { for(int i = 0 ; i< num.length() ; i++) { if( i==0 && num.charAt(i) == β€˜0’) { continue; } if( num.charAt(i) - β€˜0’ > β€˜9’ - num.charAt(i) ) { int temp = β€˜9’ - num.charAt(i); System.out.print(temp); } else { int temp = num.charAt(i) - β€˜0’; System.out.print(temp); } } } } }

Please save your code on online ide… Sending code like this changes the code and i cannot even run the code .

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.