What is wrong in the code?

import java.util.*;
import java.lang.Math;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int m,n,hypo,base,h;
if (N%2==0){
m = N/2;
n = 1;
hypo=(int)Math.pow(m,2)+(int)Math.pow(n,2);
h=(int)Math.pow(m,2)-(int)Math.pow(n,2);
if(hypo>h){
System.out.println(h+" “+hypo);
}
else{
System.out.println(hypo+” "+h);

		 }}
	else if(N%2==1){
		m=(N+1)/2;
		n=(N-1)/2;
		hypo=(int)Math.pow(m,2)+(int)Math.pow(n,2);
		base=2*m*n;
		if(hypo>base){
			System.out.println(base+" "+hypo);
		}
		else{
			System.out.println(hypo+" "+base);
		}
	

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




		 
	}

}

@mishikasrvastava,

First, suppose n is the shortest side of the triangle, m, k are other two sides. According to Pythagorean Theorem, we know n^2 + m^2 = k^2

just do a change k^2 - m^2 = n^2

futherly (k + m)*(k - m) = n^2

as we know, n^2 × 1 = n^2 so we can suppose that k + m = n^2, k - m = 1 [because we have (k + m)(k - m) = n^2, we can write this as (k + m)(k - m) = n^2]

easily, we get:

because the side is a interger, so this solution can only be used when n is a odd.

So how to deal with even? we find that if (k-m) is odd, the solution is suitable for odd. On the other hand, we guess that if(k-m) is even, the solution is suitable for even.

just as this:

so, we get:

this is an O(1) algorithm.

Use long in the code entirely. I hope it helps :slight_smile: