Pythagorean Triplet | Practice | GeeksforGeeks

Given an array arr of N integers, write a function that returns true if there is a triplet (a, b, c) that satisfies a2 + b2 = c2, otherwise false.
Example 1:
Input:
N = 5
Arr[] = {3, 2, 4, 6, 5}
Output: Yes
Explanation: a=3, b=4, and c=5 forms a
pythagorean triplet.

Example 2:
Input:
N = 3
Arr[] = {3, 8, 5}
Output: No
Explanation: No such triplet possible.

Your Task:
Complete the function checkTriplet() which takes an array arr, single integer n, as input parameters and returns boolean denoting answer to the problem. You don't to print answer or take inputs. 
Note: The driver will print "Yes" or "No" instead of boolean.
Expected Time Complexity: O(max(Arr[i])2)
Expected Auxiliary Space: O(max(Arr[i]))
Constraints:
1 <= N <= 107
1 <= Arr[i] <= 1000
 
0
abhigyanpatek4 days ago
Simple C++ Solution: Time - O(n^2) & Space - O(n)
I used unordered_set for faster lookup's!
bool checkTriplet(int arr[], int n) {    unordered_set<int> set;    for(int i = 0; i<n; i++)        set.insert(arr[i]*arr[i]);    for(int i = 0; i<n-1; i++){        for(int j = i+1; j<n; j++){            int sum = arr[i]*arr[i] + arr[j]*arr[j];            if(set.find(sum) != set.end())                return true;        }    }    return false;}
+1
harshscode1 week ago
O(n^2) and O(1).. using 2 pointer approach...........
 sort(a,a+n);    for(int i=n-1;i>1;i--)   {       int j=0;       int k=i-1;       while(j<k)       {     if(a[k]*a[k]+a[j]*a[j]<a[i]*a[i])     j++;     else if(a[k]*a[k]+a[j]*a[j]>a[i]*a[i])     k--;     else     return true;   }   }    return false;}
-1
kkumarakash70112 weeks ago
Total Time Taken:
0.49/1.61
 
 bool checkTriplet(int arr[], int n) {        int a[n];    for(int i=0;i<n;i++)    {        a[i]=pow(arr[i],2);    }        unordered_set<int> u;    for(int i=0;i<n;i++)    {        u.insert(a[i]);    }        for(int i=0;i<n-1;i++)    {        for(int j=i+1;j<n;j++)        {            if(u.find(a[i]+a[j])!=u.end())            return true;        }    }    return false;    // code here}
0
mestryruturaj2 weeks ago
Java: O(n*n)
class Solution {
    boolean checkTriplet(int[] A, int n) {
        Arrays.sort(A);
        
        for (int i = n - 1; i >= 0; i--) {
            int left = 0;
            int right = i - 1;
            while (left < right) {
                int LHS = (int)Math.pow(A[i], 2);
                int RHS = (int)Math.pow(A[left], 2) + (int)Math.pow(A[right], 2);
                
                if (LHS == RHS) return true;
                else if (LHS < RHS) right--;
                else left++;
            }
        }
        
        return false;
    }
}
+1
zerefkhan2 weeks ago
C++ solution
Total Time Taken:
0.31/1.61
	bool checkTriplet(int arr[], int n) {
	    if(n<3) return false;
	    for(int i=0;i<n-2;i++){
	        for(int j=i;j<n-1;j++){
	            for(int k=j;k<n;k++){
	                if((arr[i]*arr[i] + arr[j]*arr[j] - arr[k]*arr[k]) == 0){return true;}
	                else if((arr[j]*arr[j] + arr[k]*arr[k] - arr[i]*arr[i]) == 0){return true;}
	                else if((arr[i]*arr[i] - arr[j]*arr[j] + arr[k]*arr[k]) == 0){return true;}
	            }
	        }
	    }
	    return false;
	}
-3
amoghyelasangi3 weeks ago
ONLY 4 TEST CASES PASSED
for a in range(len(arr)):        for b in range(len(arr)):            for c in range(len(arr)):                if a**2 + b**2 == c**2:                    return "Yes"                else:                    return "No"
0
swapniltayal4224 weeks ago
public:// Function to check if the// Pythagorean triplet exists or notbool checkTriplet(int arr[], int n) {    // code here    sort(arr, arr+n);    if (n < 3){        return false;    }        for (int i=0; i<n; i++){        for (int j=i+1; j<n; j++){            for (int k=j+1; k<n; k++){                int a=arr[i]; int b=arr[j]; int c=arr[k];                if ((a*a)+(b*b) == (c*c)){                    return true;                }            }        }    }return false;}
-2
abhishekaich20001 month ago
Simple c++ solution
 
bool checkTriplet(int arr[], int n) {    // code here    for (int i=0; i<n; i++){        arr[i] *= arr[i];    }    sort(arr, arr+n);    int high=0, low=0;    for(int i=n-1; i>=0; i--){        high = i-1;        low = 0;        int temp = arr[i];        while (low < high){            int a1 = arr[low], a2 = arr[high];            if ((temp - (a1 + a2)) == 0){                return true;            }else if ((temp - (a1 + a2)) > 0){                low++;            }else{                high--;            }        }    }    return false;}
-2
charanvallala71 month ago
class Solution{public:// Function to check if the// Pythagorean triplet exists or notbool checkTriplet(int arr[], int n) {    sort(arr,arr+n);    if(n<3){        return false;    }    for(int i=0;i<n-2;i++){        for(int j=i+1;j<n-1;j++){            for(int k=j+1;k<n;k++){                int a=arr[i],b=arr[j],c=arr[k];                if((a*a)+(b*b)==(c*c)){                return true;                }            }        }   }   return false;// code here}
0
atif836141 month ago
JAVA SOLUTION:
class Solution {   boolean checkTriplet(int[] arr, int n) {       if (arr.length == 0) {  return false; } Arrays.sort(arr); for (int i = 0; i < arr.length; i++) {  for (int j = i + 1; j < arr.length; j++) {   for (int k = j + 1; k < arr.length; k++) {    if (arr[i] * arr[i] + arr[j] * arr[j] == arr[k] * arr[k]) {     return true;    }   }  } } return false;   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.