Scanner sc = new Scanner(System.in);
long n = sc.nextInt();
int b,c;
// if n is odd
if (n % 2 != 0) {
if(n==1) {
System.out.println("-1");
}
b = (int)(n * n - 1) / 2;
c = (int)(n * n + 1) / 2;
} else {
if(n == 2) {
System.out.println("1");
}
b = (int)(n * n / 4 - 1);
c = (int)(n * n / 4 + 1);
}
if(b>c) {
System.out.println( c + " " + b);
}else {
System.out.println( b + " " + c);
}
What is wrong in my code?
Change data Type int to long
try for this input
99999993
correct output :
4999999300000024 4999999300000025
for input 1 output should be -1 .add return here:
if(n==1 || n==0){
System.out.println(-1);
return;
}
corrected code:
please mark your doubt as resolved 