import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int first = sc.nextInt();
int second = sc.nextInt();
int diff = first - second;
int no_of_digits = Integer.toString(diff).length();
if (no_of_digits == 1)
{
if(diff == 0)
diff ++;
else
diff --;
}
else //multi digit number
{
String str = String.valueOf(diff);
int index = str.indexOf('0');
if(index == -1) // no zero in number
{
diff -- ;
}
else // 0 is present in the answer
{
StringBuffer sb = new StringBuffer(str);
sb.setCharAt(index,'9');
str = sb.toString();
diff = Integer.parseInt(str);
}
}
System.out.println(diff);
}
}
Please format your code (use the </> “Preformatted Text” in the edit box :))
Edit:
My initial answer was wrong - withdrawn
Edit2:
Aha -
Your answer must be a positive integer
What is your output for
31 30
?
You can do something better than this because MD provide the code embadding on basis of language which provide syntax highlighting too.
You can do something like this
```programming language you want like cpp Then your code should be here ```
1 Like
my answer for 31 30 is 0
Is that a positive integer?
Ooh, thanks - testing:
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int first = sc.nextInt();
int second = sc.nextInt();
int diff = first - second;
int no_of_digits = Integer.toString(diff).length();
if (no_of_digits == 1)
{
if(diff == 0)
diff ++;
else
diff --;
}
else //multi digit number
{
String str = String.valueOf(diff);
int index = str.indexOf('0');
if(index == -1) // no zero in number
{
diff -- ;
}
else // 0 is present in the answer
{
StringBuffer sb = new StringBuffer(str);
sb.setCharAt(index,'9');
str = sb.toString();
diff = Integer.parseInt(str);
}
}
System.out.println(diff);
}
}
Nice - thanks for the info!
Oh my god…
this was the problem
0 is neither positive nor negative integer and i missed that
Thank you very much for pointing it out …
1 Like