Why all test cases are not runnning. Is it because i used function for calculating power and typecasting?

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int base1 = n, exponent1 = 2;
int base2 = n / 2, exponent2 = 2;
double expo = Math.pow(base1, exponent1);
double expo2 = Math.pow(base2, exponent2);
if (n % 2 != 0) {
double Num1 = (expo / 2) - 0.5f;
double Num2 = (expo / 2) + 0.5f;
System.out.print( Math.round(Num1) + " " + Math.round(Num2));
} else {
double Num3 = expo2 - 1;
double Num4 = expo2 + 1;
System.out.print( Math.round(Num3) + " " + Math.round(Num4));

	}




}

}

Hey @kalindiyadav5
try for this input
99999993
your code Gives : 4999999300000024 4999999300000024
correct output : 4999999300000024 4999999300000025
and put this condition
Print its Pythagoras pair in increasing order if they exist. Otherwise, print “-1”.
simple solution for this Question
public static void pythagoras(long n) {
if (n == 1 || n == 2) {
System.out.println(-1);
} else if (n % 2 == 0) {
System.out.println(((n * n) / 4) - 1 + " " + (((n * n) / 4) + 1));
} else if (n % 2 != 0) {
System.out.println((n * n - 1) / 2 + " " + (n * n + 1) / 2);
}
}

1 Like

why my code is giving this output??

Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int base1 = n, exponent1 = 2;
int base2 = n / 2, exponent2 = 2;
double expo = Math.pow(base1, exponent1);
double expo2 = Math.pow(base2, exponent2);
if (n % 2 != 0) {
double Num1 = (expo / 2) - 0.5f;
double Num2 = (expo / 2) + 0.5f;
System.out.print( Math.round(Num1) + " " + Math.round(Num2));
}else if(n%2==0) {
double Num3 = expo2 - 1;
double Num4 = expo2 + 1;
System.out.print( Math.round(Num3) + " " + Math.round(Num4));
}else {
System.out.print("-1");
}

exactly where is my fault that the test cases are not running

Even for this code all the test cases are not running

@kalindiyadav5
I have verified

@kalindiyadav5
this is your submitted code
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
if (n == 1 || n == 2) {
System.out.println(-1);
} else if (n % 2 == 0) {
System.out.println(((n * n) / 4) - 1 + " " + (((n * n) / 4) + 1));
} else if (n % 2 != 0) {
System.out.println((n * n - 1) / 2 + " " + (n * n + 1) / 2);
}

}

}
just a change long n = scn.nextLong(); instead of int n = scn.nextInt();

Yes . Thank you now it is working.
Also i want to know that why was my code not passing all the test cases

@kalindiyadav5
I think Math.round change your answer
becuzz answer is to longer.
I am not sure

Okay. If u find the exact answer then please tell me . thank you

@kalindiyadav5
Sure I will tell you

1 Like