Check for Pythagorean Triplet in array
Problem : check for a^2=b^2+c^2
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static boolean check(int arr[],int n)
{
Arrays.sort(arr);
for(int i=n-1;i>=2;i--)
{
int l=0;
int r=i-1;
while(l<r){
if(arr[i]==arr[l]+arr[r])
return true;
else if(arr[i]>arr[l]+arr[r])
l++;
else if(arr[i]<arr[l]+arr[r])
r--;}
}
return false;
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{arr[i]=ab.nextInt();
arr[i]=arr[i]*arr[i];
}
if(check(arr,n)==true)
System.out.println("Yes");
else
System.out.println("No");
}
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static boolean check(int arr[],int n)
{
Arrays.sort(arr);
for(int i=n-1;i>=2;i--)
{
int l=0;
int r=i-1;
while(l<r){
if(arr[i]==arr[l]+arr[r])
return true;
else if(arr[i]>arr[l]+arr[r])
l++;
else if(arr[i]<arr[l]+arr[r])
r--;}
}
return false;
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{arr[i]=ab.nextInt();
arr[i]=arr[i]*arr[i];
}
if(check(arr,n)==true)
System.out.println("Yes");
else
System.out.println("No");
}
}
}
0 Comments:
Post a Comment