Time Limit Exceed

Sir/Mam
How do i resolve the issue of TLE in this program?

import java.util.*;
public class Main {
public static void main(String args[]) {

    Scanner obj = new Scanner(System.in);
    int n = obj.nextInt();

    if(n<=Math.pow(10,9)){
        int i;
        for(i=3; i<=n*n; i++){
            for(int j=3; j<=i; j++){
                if((i*i+j*j)==n*n){
                    System.out.println(j+" "+i);
                    break;
                }else if((i*i+n*n)==j*j){
                    System.out.println(i+" "+j);
                    break;
                }else if((j*j+n*n)==i*i){
                    System.out.println(j+" "+i);
                    break;
                }
            }
        }
        if(i==100*n){
            System.out.println("-1");
        }
    }
}

}

your codes complexity is of the order of n^2…

Pythagorean triplet are of form m^2-n^2, 2mn , m^2+n^2…in this ques you are given a value…assume that this value can be of first form or second form…so if the given value say ‘a’ is even then you can equate it to 2mn…mn=a/2…there can be various values of (m,n)…you can take any value of (m,n)… one possible value can be m=a/2 and n=1…else if the given value ‘a’ is odd equate it to m^2-n^2…by factorization m^2-n^2=(m+n)(m-n)…(m+n)(m-n)=a…there could be many values of (m,n)…you can chose any one…(m+n)=a and (m-n)=1 solving these two equations we get m=(a+1)/2 and n=(a-1)/2…as you know the values of m,n rest of the two pythagorean triplets can be found…

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.

actually don’t need to find this by using loop.
u have to just find a formula to find the pythog. triplet of a given no.

        // Calculating for even case
        System.out.println(((n * n) / 4) - 1 + " " + (((n * n) / 4) + 1));

       //for odd case 
        System.out.println((n * n - 1) / 2 + " " + (n * n + 1) / 2);

you can find the logic on the net.
hope this will help u

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.