Pythagorus Triplet // test cases

import java.util.Scanner;

public class Pythagorus_triplet1 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	if(n%2==0) {

	int m=n/2;
	int per= (int) Math.pow(m, 2)-1;
	int hypo = (int) Math.pow(m, 2)+1;
	if (n<per && n<hypo) {
		System.out.print(per+" ");
		System.out.print(hypo);
	}
		else {
			System.out.print("-1");
		}
	}
	else if(n%2 !=0) {
		
		int m= (int) Math.sqrt(n+1);
		int base = 2 *m;
		int per= (int) Math.pow(m, 2)-1;
		
		int hypo = (int) Math.pow(m, 2)+1;
		
		if(per<base && per<hypo){
			System.out.print(base+" ");
			System.out.print(hypo);

}
	else {
		System.out.print("-1");
	}
}
}

}

1 Like

import java.util.Scanner; public class Pythagorus_Triplet { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scn = new Scanner(System.in); int n = scn.nextInt(); if(n%2!=0) { int base = (int) Math.pow(n, 2); int rem1 = base/2; int per = (int) Math.pow(rem1, 2); int rem = base-rem1; int hypo= (int) Math.pow(rem, 2); if(hypo == (base)+(per) && n<rem1 && n<rem ){ System.out.print(rem1+" “); System.out.print(rem); } else { System.out.print(”-1"); } }else { int base= (int) Math.pow(n,2); int rem= n/2; int sol= rem*rem; int per= (int) Math.pow(sol-1, 2); int hypo= (int) Math.pow(sol+1, 2); if(hypo == (base)+(per) && n<(sol-1) && n<(sol+1)){ System.out.print(sol-1+" “); System.out.print(sol+1); } else { System.out.print(”-1"); } } } }

1 Like

Hi @anktre7 , since the constraint of the question is n <= 10^9, just change the datatype from int to long.

1 Like

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.

Done but unable to pass test case 3 and test case 4
import java.util.*;
public class Main {
public static void main(String args[]) {

	// TODO Auto-generated method stub
	Scanner scn = new Scanner(System.in);
	long n = scn.nextLong();
	if(n%2!=0 && n>0) {         
		long base = (long) Math.pow(n, 2);  
		
		long rem1 = base/2;  
		
		long per = (long) Math.pow(rem1, 2);
		
		long rem = base-rem1;  
		
		long hypo= (long) Math.pow(rem, 2);
		
		if(hypo == (base)+(per) && n<rem1 && n<rem ){
		System.out.print(rem1+" ");
		System.out.print(rem);
		}
		else {
			System.out.print("-1");
		}
	}	else if(n%2==0 && n>0) {
			
		long base= (long) Math.pow(n,2);
		long rem= n/2;
		long sol= rem*rem;
		long per= (long) Math.pow(sol-1, 2);
		long hypo= (long) Math.pow(sol+1, 2);
		if(hypo == (base)+(per) && n<(sol-1) && n<(sol+1)){
			System.out.print(sol-1+" ");
			System.out.print(sol+1);
			}
		else {
			System.out.print("-1");
		}
		
	}
	else
	{
		System.out.print("-1");
	}
}

}