import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int N = n ;
int count = 0 ;
if (n==0){
System.out.print(ā0ā);
}
else{
while (n!=0){
n = n/10 ;
count++;
}
int []arr = new int[count];
for (int i = count-1 ; i >= 0 ; iā){
arr[i] = N % 10 ;
N = N/10 ;
}
for (int i = 0 ; i < count ; i++){
if (arr[i] >= 5){
arr[i] = 9-arr[i];
}
}
for (int i = 0 ; i < count ; i++){
System.out.print(arr[i]);
}
}
}
}
What is the error in the code ? It is not passsing three test cases.
Chewbacca and Number
you are missing the corner cases. as problem stmt says that there should not be any leading zeros.
check your code for cases like 987 or 999.
you must check first digit separately. as the output for 987 and 999 will be 912 and 900 respectively.
Thanks
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in) ;
int n = s.nextInt();
int N = n ;
int count = 0 ;
while (n > 0){
n = n/10 ;
count++ ;
}
int []arr = new int [count] ;
for (int i = count-1 ; i >=0 ; iā){
arr[i] = N % 10 ;
N = N/10 ;
}
for (int i = 0 ; i < count ; i++){
if ((i == 0) && (arr[i] != 9) && (arr[i] >= 5)){
arr[i] = 9 - arr[i] ;
}
if ((arr[i] >= 5) && (i > 0)){
arr[i] = 9 - arr[i] ;
}
}
for (int i = 0 ; i < count ; i++){
System.out.print(arr[i]);
}
}
}
I have made changes in the code but still two test cases are giving runtime error .
Pls tell what should be the required changes .
@Sejalrathi try and dry run your code for 932711047202 .
your code gives 9322110420-20.it should be 932211042202.
acc. to the logic of my code , the answer is coming right but while running it on eclipse for the above input it is showing input mismatch error.
@Sejalrathi InputMismatchException is thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.
the constraints are larger than int. that is why your input is being read wrong
@Sejalrathi refer to this for the correct code
please mark your doubt as resolved if u are satisfied