Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
3 Sum - Pythagorean Triplet in an array
Next article icon

3 Sum - Pythagorean Triplet in an array

Last Updated : 21 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given an array of positive integers, the task is to determine if a Pythagorean triplet exists in the given array. A triplet {a, b, c} is considered a Pythagorean triplet if it satisfies the condition a2 + b2 = c2.

Example:

Input: arr[] = [3, 1, 4, 6, 5] 
Output: true
Explanation: The array contains a Pythagorean triplet {3, 4, 5}.

Input: arr[] = [10, 4, 6, 12, 5]
Output: false 
Explanation: There is no Pythagorean triplet. 

Table of Content

  • [Naive Approach] Explore all the triplets - O(n^3) Time and O(1) Space
  • [Better Approach-1] Two Pointers Technique - O(n^2) Time and O(1) Space
  • [Better Approach-2] Using Hashing - O(n^2) Time and O(n) Space
  • [Expected Approach] Iterating till max element - O(max(arr)^2) Time and O(max(arr)) Space

[Naive Approach] Explore all the triplets - O(n^3) Time and O(1) Space

The simplest approach is to explore all the triplets(a, b, c) using three nested loops and if any of them satisfies the condition of Pythagorean Triplet, that is a2 + b2 = c2 or a2 + c2 = b2 or b2 + c2 = a2, return true.

C++
// C++ program to find if a Pythagorean triplet exists  // by exploring all triplets  #include <iostream> #include <vector> using namespace std;  bool pythagoreanTriplet(vector<int> &arr) {     int n = arr.size();      // Exploring all possible triplets     for (int i = 0; i < n; i++) {         for (int j = i + 1; j < n; j++) {             for (int k = j + 1; k < n; k++) {                  // Calculate square of array elements                 int x = arr[i] * arr[i];                 int y = arr[j] * arr[j];                 int z = arr[k] * arr[k];                  // If these integers form Pythagorean triplet then                 // return true                 if (x == y + z || y == x + z || z == x + y)                     return true;             }         }     }      return false; }  int main() {     vector<int> arr = {3, 1, 4, 6, 5};   	cout << (pythagoreanTriplet(arr) ? "true" : "false");     return 0; } 
C
// C program to find if a Pythagorean triplet exists  // by exploring all triplets  #include <stdio.h> #include <stdbool.h>  bool pythagoreanTriplet(int arr[], int n) {      // Exploring all possible triplets     for (int i = 0; i < n; i++) {         for (int j = i + 1; j < n; j++) {             for (int k = j + 1; k < n; k++) {                  // Calculate square of array elements                 int x = arr[i] * arr[i];                 int y = arr[j] * arr[j];                 int z = arr[k] * arr[k];                  // If these integers form Pythagorean triplet then                 // return true                 if (x == y + z || y == x + z || z == x + y)                     return true;             }         }     }      return false; }  int main() {     int arr[] = {3, 1, 4, 6, 5};     int n = sizeof(arr) / sizeof(arr[0]);     printf(pythagoreanTriplet(arr, n) ? "true" : "false");      return 0; } 
Java
// Java program to find if a Pythagorean triplet exists  // by exploring all triplets  import java.util.*;  class GfG {     static boolean pythagoreanTriplet(int[] arr) {         int n = arr.length;          // Exploring all possible triplets         for (int i = 0; i < n; i++) {             for (int j = i + 1; j < n; j++) {                 for (int k = j + 1; k < n; k++) {                      // Calculate square of array elements                     int x = arr[i] * arr[i];                      int y = arr[j] * arr[j];                     int z = arr[k] * arr[k];                      // If these integers form Pythagorean triplet then                     // return true                     if (x == y + z || y == x + z || z == x + y)                         return true;                 }             }         }          return false;     }      public static void main(String[] args) {         int[] arr = {3, 1, 4, 6, 5};         System.out.println(pythagoreanTriplet(arr));     } } 
Python
# Python program to find if a Pythagorean triplet exists  # by exploring all triplets  def pythagoreanTriplet(arr):     n = len(arr)      # Exploring all possible triplets     for i in range(n):         for j in range(i + 1, n):             for k in range(j + 1, n):                  # Calculate square of array elements                 x = arr[i] ** 2                 y = arr[j] ** 2                 z = arr[k] ** 2                  # If these integers form Pythagorean triplet then                 # return true                 if x == y + z or y == x + z or z == x + y:                     return True      return False  if __name__ == "__main__":          arr = [3, 1, 4, 6, 5]     if pythagoreanTriplet(arr):         print("true")     else :         print("false")               
C#
// C# program to find if a Pythagorean triplet exists  // by exploring all triplets  using System;  class GfG {     static bool pythagoreanTriplet(int[] arr) {         int n = arr.Length;          // Exploring all possible triplets         for (int i = 0; i < n; i++) {             for (int j = i + 1; j < n; j++) {                 for (int k = j + 1; k < n; k++) {                      // Calculate square of array elements                     int x = arr[i] * arr[i];                     int y = arr[j] * arr[j];                     int z = arr[k] * arr[k];                      // If these integers form Pythagorean triplet then                     // return true                     if (x == y + z || y == x + z || z == x + y)                         return true;                 }             }         }          return false;     }      static void Main() {         int[] arr = {3, 1, 4, 6, 5};  		Console.WriteLine(pythagoreanTriplet(arr)?"true":"false"); 	} } 
JavaScript
// JavaScript program to find if a Pythagorean triplet exists  // by exploring all triplets  function pythagoreanTriplet(arr) {     let n = arr.length;      // Exploring all possible triplets     for (let i = 0; i < n; i++) {         for (let j = i + 1; j < n; j++) {             for (let k = j + 1; k < n; k++) {                  // Calculate square of array elements                 let x = arr[i] * arr[i];                 let y = arr[j] * arr[j];                 let z = arr[k] * arr[k];                  // If these integers form Pythagorean triplet then                 // return true                 if (x == y + z || y == x + z || z == x + y)                     return true;             }         }     }      return false; }  let arr = [3, 1, 4, 6, 5]; console.log(pythagoreanTriplet(arr)); 

Output
true

[Better Approach-1] Two Pointers Technique - O(n^2) Time and O(1) Space

The idea is to square each element of the array and sort it in ascending order. Then, we will traverse the array in reverse and fix the largest element of the triplet as c2. After that, we will use two-pointers technique to find two elements a2 and b2 in remaining array such that a2 + b2 = c2. We initialize two pointers at both ends of the remaining portion of the array (from index 0 to i−1) and adjust them based on the sum of the elements at both pointers as follows:

  • If sum = c2, we have found the Pythagorean triplet.
  • If sum < c2, we move the left pointer towards right.
  • If sum > c2, we move the right pointer towards left.


C++
// C++ program to find if a Pythagorean triplet   // exists using the two-pointer technique  #include <iostream> #include <algorithm> #include <vector> using namespace std;  bool pythagoreanTriplet(vector<int> &arr) {   	int n = arr.size();      	// Taking Square of each element     for (int i = 0; i < n; i++)         arr[i] = arr[i] * arr[i];     sort(arr.begin(), arr.end());      // fix the largest element of Pythagorean triplet     for (int i = n - 1; i > 1; i--) {          // Two pointer technique to find remaining two          // elements such that a^2 + b^2 = c^2         int l = 0;         int r = i - 1;         while (l < r) {              // A Pythagorean triplet is found             if (arr[l] + arr[r] == arr[i])                 return true;              if (arr[l] + arr[r] < arr[i])                 l++;            	else               	r--;         }     }      return false; }  int main() {     vector<int> arr = {3, 1, 4, 6, 5}; 	cout << (pythagoreanTriplet(arr) ? "true" : "false");   	     return 0; } 
C
// C program to find if a Pythagorean triplet   // exists using the two-pointer technique  #include <stdio.h> #include <stdlib.h> #include <stdbool.h>  int compare(const void *a, const void *b) {     return (*(int *)a - *(int *)b); }  bool pythagoreanTriplet(int arr[], int n) {        // Taking Square of each element     for (int i = 0; i < n; i++)         arr[i] = arr[i] * arr[i];      // Sort the array     qsort(arr, n, sizeof(int), compare);      // Fix the largest element of Pythagorean triplet     for (int i = n - 1; i > 1; i--) {                // Two pointer technique to find remaining two          // elements such that a^2 + b^2 = c^2         int l = 0;         int r = i - 1;         while (l < r) {                        // A Pythagorean triplet is found             if (arr[l] + arr[r] == arr[i])                 return true;              if (arr[l] + arr[r] < arr[i])                 l++;             else                 r--;         }     }     return false; }  int main() {     int arr[] = {3, 1, 4, 6, 5};     int n = sizeof(arr) / sizeof(arr[0]);     printf(pythagoreanTriplet(arr, n) ? "true" : "false");          return 0; } 
Java
// Java program to find if a Pythagorean triplet   // exists using the two-pointer technique  import java.util.Arrays;  class GfG {     static boolean pythagoreanTriplet(int[] arr) {         int n = arr.length;          // Taking Square of each element         for (int i = 0; i < n; i++)             arr[i] = arr[i] * arr[i];         Arrays.sort(arr);          // Fix the largest element of Pythagorean triplet         for (int i = n - 1; i > 1; i--) {                        // Two pointer technique to find remaining two              // elements Such that a^2 + b^2 = c^2             int l = 0;             int r = i - 1;             while (l < r) {                                // A Pythagorean triplet is found                 if (arr[l] + arr[r] == arr[i])                     return true;                  if (arr[l] + arr[r] < arr[i])                     l++;                 else                     r--;             }         }         return false;     }      public static void main(String[] args) {         int[] arr = {3, 1, 4, 6, 5};         System.out.println(pythagoreanTriplet(arr));              } } 
Python
# Python program to find if a Pythagorean triplet   # exists using the two-pointer technique  def pythagoreanTriplet(arr):     n = len(arr)      # Taking Square of each element     for i in range(n):         arr[i] = arr[i] * arr[i]     arr.sort()      # Fix the largest element of Pythagorean triplet     for i in range(n - 1, 1, -1):                # Two pointer technique to find remaining two          # elements such that a^2 + b^2 = c^2         l = 0         r = i - 1         while l < r:                        # A Pythagorean triplet is found             if arr[l] + arr[r] == arr[i]:                 return True              if arr[l] + arr[r] < arr[i]:                 l += 1             else:                 r -= 1     return False  if __name__ == "__main__":     arr = [3, 1, 4, 6, 5]     print("true" if pythagoreanTriplet(arr) else "false")      
C#
// C# program to find if a Pythagorean triplet   // exists using the two-pointer technique  using System;  class GfG {     static bool pythagoreanTriplet(int[] arr) {         int n = arr.Length;          // Taking Square of each element         for (int i = 0; i < n; i++)             arr[i] = arr[i] * arr[i];         Array.Sort(arr);          // Fix the largest element of Pythagorean triplet         for (int i = n - 1; i > 1; i--) {                        // Two pointer technique to find remaining two elements             // such that a^2 + b^2 = c^2             int l = 0;             int r = i - 1;             while (l < r) {                                // A Pythagorean triplet is found                 if (arr[l] + arr[r] == arr[i])                     return true;                  if (arr[l] + arr[r] < arr[i])                     l++;                 else                     r--;             }         }         return false;     }      static void Main() {         int[] arr = {3, 1, 4, 6, 5};         Console.WriteLine(pythagoreanTriplet(arr)?"true":"false");     } } 
JavaScript
// JavaScript program to find if a Pythagorean triplet   // exists using the two-pointer technique  function pythagoreanTriplet(arr) {     let n = arr.length;      // Taking Square of each element     for (let i = 0; i < n; i++)         arr[i] = arr[i] * arr[i];     arr.sort((a, b) => a - b);      // Fix the largest element of Pythagorean triplet     for (let i = n - 1; i > 1; i--) {              // Two pointer technique to find remaining two          // elements such that a^2 + b^2 = c^2         let l = 0;         let r = i - 1;         while (l < r) {                      // A Pythagorean triplet is found             if (arr[l] + arr[r] === arr[i])                 return true;              if (arr[l] + arr[r] < arr[i])                 l++;             else                 r--;         }     }     return false; }  // Driver Code const arr = [3, 1, 4, 6, 5]; console.log(pythagoreanTriplet(arr)); 

Output
true

[Better Approach-2] Using Hashing - O(n^2) Time and O(n) Space

First, we will insert all the elements of the given array into a hash set. Then, using two nested loops, we will iterate through all possible combinations of a and b, and check if there exists a third value c that forms a Pythagorean triplet with a and b. If such c exists, we will return true.

C++
// C++ program to find if a Pythagorean triplet exists  // using hashing  #include <iostream> #include <vector> #include <unordered_set> #include <math.h>  using namespace std;  bool pythagoreanTriplet(vector<int> &arr) {    	int n = arr.size();     unordered_set<int> st;     for (int i = 0; i < n; i++)         st.insert(arr[i]);    	// Iterate through all possible values of a and b     for (int i = 0; i < n - 1; i++)  {         for (int j = i + 1; j < n; j++) {           	           	int a = arr[i];           	int b = arr[j];                        // calculate required value for            	// c to form Pythagorean triplet             int c = sqrt(a * a + b * b);                          // First, verify if c^2 is a perfect square (indicating a  			// valid c) and check if this c exists in the set.             if (c*c == a*a + b*b && st.find(c) != st.end())                 return true;         }     }      	// No Pythagorean triplet exists in the array     return false; }  int main() {     vector<int> arr = {3, 1, 4, 6, 5};     cout << (pythagoreanTriplet(arr) ? "true" : "false");   	     return 0; } 
Java
// Java program to find if a Pythagorean triplet exists  // using hashing  import java.util.HashSet;  class GfG {     static boolean pythagoreanTriplet(int[] arr) {        	int n = arr.length;         HashSet<Integer> st = new HashSet<>();         for (int i = 0; i < n; i++)             st.add(arr[i]);          // Iterate through all possible values of a and b         for (int i = 0; i < n - 1; i++) {             for (int j = i + 1; j < n; j++) {                                  	int a = arr[i];           		int b = arr[j];                        	// calculate required value for            		// c to form Pythagorean triplet             	int c = (int)Math.sqrt(a * a + b * b);                          	// First, verify if c^2 is a perfect square (indicating a  				// valid c) and check if this c exists in the set.             	if (c*c == a*a + b*b && st.contains(c))                 	return true;             }         }                // No Pythagorean triplet exists in the array         return false;     }      public static void main(String[] args) {         int[] arr = {3, 1, 4, 6, 5};         System.out.println(pythagoreanTriplet(arr));     } } 
Python
# Python program to find if a Pythagorean triplet exists # using hashing  def pythagoreanTriplet(arr):     n = len(arr)     st = set()          for i in range(n):         st.add(arr[i])      # Iterate through all possible values of a and b     for i in range(n - 1):         for j in range(i + 1, n):                        a = arr[i]             b = arr[j]                        # calculate required value for              # c to form Pythagorean triplet             c = (int)((a ** 2 + b ** 2) ** 0.5)                          # First, verify if c^2 is a perfect square (indicating a              # valid c) and check if this c exists in the set.             if (c*c == a*a + b*b) and (c in st):                 return True                  # No Pythagorean triplet exists in the array     return False  if __name__ == "__main__":     arr = [3, 1, 4, 6, 5]     print("true" if pythagoreanTriplet(arr) else "false") 
C#
// C# program to find if a Pythagorean triplet exists  // using hashing  using System; using System.Collections.Generic;  class GfG {     static bool pythagoreanTriplet(int[] arr) {          HashSet<int> st = new HashSet<int>();         int n = arr.Length;         for (int i = 0; i < n; i++)             st.Add(arr[i]);          // Iterate through all possible values of a and b         for (int i = 0; i < n - 1; i++) {             for (int j = i + 1; j < n; j++) {                              int a = arr[i];           	int b = arr[j];                        // calculate required value for            	// c to form Pythagorean triplet             int c = (int)Math.Sqrt(a * a + b * b);                          // First, verify if c^2 is a perfect square (indicating a  			// valid c) and check if this c exists in the set.             if (c*c == a*a + b*b && st.Contains(c))                 return true;             }         }                // No Pythagorean triplet exists in the array         return false;     }      static void Main() {         int[] arr = {3, 1, 4, 6, 5};         Console.WriteLine(pythagoreanTriplet(arr)?"true":"false");     } } 
JavaScript
// JavaScript program to find if a Pythagorean triplet exists  // using hashing  function pythagoreanTriplet(arr) {     const st = new Set();     const n = arr.length;     for (let i = 0; i < n; i++)         st.add(arr[i]);      // Iterate through all possible values of a and b     for (let i = 0; i < n - 1; i++) {         for (let j = i + 1; j < n; j++) {                   	const a = arr[i]             const b = arr[j]                          // calculate required value for            	// c to form Pythagorean triplet             const c = Math.sqrt(a * a + b * b);                          // First, verify if c^2 is a perfect square (indicating a  			// valid c) and check if this c exists in the set.             if (c*c === a*a + b*b && st.has(c))                 return true;         }     }        // No Pythagorean triplet exists in the array     return false; }  const arr = [3, 1, 4, 6, 5]; console.log(pythagoreanTriplet(arr) ? "true" : "false"); 

Output
true

[Expected Approach] Iterating till max element - O(max(arr)^2) Time and O(max(arr)) Space

The idea is to generate all possible pairs (a, b) within the range of maximum element in the input array using nested loops. For each pair, calculate the value of c required to form a Pythagorean Triplet and check if c exists in the input array. We can check if c exists or not in constant time by marking all the elements of input array in a visited array.

Here, we only need to mark the elements and not store their frequency because for all valid triplets of positive integers (a, b, c), no two elements can be equal, that is a != b, a != c and b != c.

Note: This approach is more efficient than the previous ones, given the constraint (max(arr) < n).

C++
// C++ program to find if a Pythagorean triplet   // exists by iterating till max element  #include <iostream> #include <vector> #include <cmath>  using namespace std;  bool pythagoreanTriplet(vector<int> &arr) {   	int n = arr.size();     int maxEle = 0;     for (int ele: arr)          maxEle = max(maxEle, ele);      // Visited array to mark the elements     vector<bool> vis(maxEle + 1, 0);      // Marking each element of input array     for (int ele: arr)         vis[ele] = true;      // Iterate for all possible a     for (int a = 1; a < maxEle + 1; a++) {          // If a is not there         if (vis[a] == false)             continue;          // Iterate for all possible b         for (int b = 1; b < maxEle + 1; b++) {              // If b is not there         	if (vis[b] == false)             	continue;              // calculate c value to form a pythagorean triplet             int c = sqrt(a * a + b * b);              // If c^2 is not a perfect square or c exceeds the           	// maximum value             if ((c * c) != (a * a + b * b) || c > maxEle)                 continue;              // If there exists c in the original array,             // we have found the triplet             if (vis[c] == true) {                 return true;             }         }     }     return false; }  int main() {     vector<int> arr = {3, 1, 4, 6, 5};   	cout << (pythagoreanTriplet(arr) ? "true" : "false");   	     return 0; } 
Java
// Java program to find if a Pythagorean triplet   // exists by iterating till max element  import java.util.Arrays;  class GfG {     static boolean pythagoreanTriplet(int[] arr) {         int n = arr.length;         int maxEle = 0;         for (int ele : arr)              maxEle = Math.max(maxEle, ele);          // Visited array to mark the elements         boolean[] vis = new boolean[maxEle + 1];          // Marking each element of input array         for (int ele : arr)             vis[ele] = true;          // Iterate for all possible a         for (int a = 1; a < maxEle + 1; a++) {              // If a is not there             if (!vis[a])                 continue;              // Iterate for all possible b             for (int b = 1; b < maxEle + 1; b++) {                  // If b is not there                 if (!vis[b])                     continue;                  // calculate c value to form a pythagorean triplet                 int c = (int) Math.sqrt(a * a + b * b);                  // If c^2 is not a perfect square or c exceeds the                 // maximum value                 if ((c * c) != (a * a + b * b) || c > maxEle)                     continue;                  // If there exists c in the original array,                 // we have found the triplet                 if (vis[c]) {                     return true;                 }             }         }         return false;     }      public static void main(String[] args) {         int[] arr = {3, 1, 4, 6, 5};         System.out.println(pythagoreanTriplet(arr));     } } 
Python
# Python program to find if a Pythagorean triplet   # exists by iterating till max element  import math  def pythagoreanTriplet(arr):     n = len(arr)     maxEle = 0     for ele in arr:         maxEle = max(maxEle, ele)      # Visited array to mark the elements     vis = [False] * (maxEle + 1)      # Marking each element of input array     for ele in arr:         vis[ele] = True      # Iterate for all possible a     for a in range(1, maxEle + 1):          # If a is not there         if not vis[a]:             continue          # Iterate for all possible b         for b in range(1, maxEle + 1):              # If b is not there             if not vis[b]:                 continue              # calculate c value to form a pythagorean triplet             c = int(math.sqrt(a * a + b * b))              # If c^2 is not a perfect square or c exceeds the             # maximum value             if (c * c) != (a * a + b * b) or c > maxEle:                 continue              # If there exists c in the original array,             # we have found the triplet             if vis[c]:                 return True      return False  if __name__ == "__main__":     arr = [3, 1, 4, 6, 5]     ans = pythagoreanTriplet(arr)          if ans:         print("true")     else :         print("false") 
C#
// C# program to find if a Pythagorean triplet   // exists by iterating till max element  using System; using System.Collections.Generic;  class GfG {     static bool pythagoreanTriplet(int[] arr) {         int n = arr.Length;         int maxEle = 0;         foreach (int ele in arr)             maxEle = Math.Max(maxEle, ele);          // Visited array to mark the elements         bool[] vis = new bool[maxEle + 1];          // Marking each element of input array         foreach (int ele in arr)             vis[ele] = true;          // Iterate for all possible a         for (int a = 1; a < maxEle + 1; a++) {                          // If a is not there             if (vis[a] == false)                 continue;              // Iterate for all possible b             for (int b = 1; b < maxEle + 1; b++) {                                  // If b is not there                 if (vis[b] == false)                     continue;                  // calculate c value to form a pythagorean triplet                 int c = (int)Math.Sqrt(a * a + b * b);                  // If c^2 is not a perfect square or c exceeds the                 // maximum value                 if ((c * c) != (a * a + b * b) || c > maxEle)                     continue;                  // If there exists c in the original array,                 // we have found the triplet                 if (vis[c] == true)                     return true;             }         }         return false;     }      static void Main() {         int[] arr = { 3, 1, 4, 6, 5 };         bool ans = pythagoreanTriplet(arr);                  if (ans)             Console.WriteLine("true");         else             Console.WriteLine("false");                  } } 
JavaScript
// JavaScript program to find if a Pythagorean triplet   // exists by iterating till max element  function pythagoreanTriplet(arr) {     let n = arr.length;     let maxEle = 0;     for (let ele of arr)          maxEle = Math.max(maxEle, ele);      // Visited array to mark the elements     let vis = new Array(maxEle + 1).fill(false);      // Marking each element of input array     for (let ele of arr)         vis[ele] = true;      // Iterate for all possible a     for (let a = 1; a < maxEle + 1; a++) {          // If a is not there         if (vis[a] === false)             continue;          // Iterate for all possible b         for (let b = 1; b < maxEle + 1; b++) {              // If b is not there             if (vis[b] === false)                 continue;              // calculate c value to form a pythagorean triplet             let c = Math.floor(Math.sqrt(a * a + b * b));              // If c^2 is not a perfect square or c exceeds the             // maximum value             if ((c * c) !== (a * a + b * b) || c > maxEle)                 continue;              // If there exists c in the original array,             // we have found the triplet             if (vis[c] === true) {                 return true;             }         }     }     return false; }  const arr = [3, 1, 4, 6, 5]; console.log(pythagoreanTriplet(arr)); 

Output
true



Next Article
3 Sum - Pythagorean Triplet in an array

H

Harshit Gupta
Improve
Article Tags :
  • DSA
  • Arrays
  • Amazon
Practice Tags :
  • Amazon
  • Arrays

Similar Reads

    Twin Pythagorean triplets in an array
    Given an array of integers arr[], the task is to check if there is a Twin Pythagorean Triplet in the array.A Twin Pythagorean Triplet is a Pythagorean triplet (a, b, c) for which two values are consecutive integers. The first few twin triples are (3, 4, 5), (5, 12, 13), (7, 24, 25), (20, 21, 29), (9
    13 min read
    Find Pythagorean Triplet with given sum
    Given a positive integer target, the task is to find all Pythagorean Triplets whose sum of the elements is equal to the given target. A triplet {a, b, c} is considered a Pythagorean triplet if it satisfies the condition a2 + b2 = c2.Examples : Input: target = 60Output: {{10, 24, 26}, {15, 20, 25}}Ex
    11 min read
    3 Sum - Triplet Sum in Array
    Given an array arr[] of size n and an integer sum, the task is to check if there is a triplet in the array which sums up to the given target sum.Examples: Input: arr[] = [1, 4, 45, 6, 10, 8], target = 13Output: true Explanation: The triplet [1, 4, 8] sums up to 13Input: arr[] = [1, 2, 4, 3, 6, 7], t
    15 min read
    Generate Pythagorean Triplets
    Given a positive integer limit, your task is to find all possible Pythagorean Triplet (a, b, c), such that a <= b <= c <= limit.Note: A Pythagorean triplet is a set of three positive integers a, b, and c such that a2 + b2 = c2. Input: limit = 20Output: 3 4 5 5 12 13 6 8 10 8 15 17 9 12 15 1
    14 min read
    POTD Solutions | 3 Nov’ 23 | Pythagorean Triplet
    View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Hashing but will also help you build up problem-solving
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences