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, 40, 41), (11, 60, 61), (13, 84, 85), (15, 112, 113), ….
Examples:
Input: arr[] = {3, 1, 4, 6, 5}
Output: True
Explanation:
There is a Twin Pythagorean triplet (3, 4, 5)Input: arr[] = {6, 8, 10}
Output: False
Explanation:
62 + 82 = 102
but in (6, 8, 10) there is no consecutive element.
So There is no Twin Pythagorean triplet.
Naive Approach: A simple solution is to run three loops, three loops pick three array elements and check if current three elements form a Twin Pythagorean Triplet.
Below is the implementation of above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if there exist // a twin pythagorean triplet in // the given array bool isTriplet( int ar[], int n) { // Loop to check if there is a // Pythagorean triplet in the array for ( int i = 0; i < n; i++) { for ( int j = i + 1; j < n; j++) { for ( int k = j + 1; k < n; k++) { // Check if there is // consecutive triple if ( abs (ar[i] - ar[j]) == 1 || abs (ar[j] - ar[k]) == 1 || abs (ar[i] - ar[k]) == 1) { // Calculate square of // array elements int x = ar[i] * ar[i], y = ar[j] * ar[j], z = ar[k] * ar[k]; if (x == y + z || y == x + z || z == x + y) return true ; } } } } return false ; } // Driver Code int main() { // Given array arr[] int arr[] = { 3, 1, 4, 6, 5 }; int ar_size = sizeof (arr) / sizeof (arr[0]); // Function Call isTriplet(arr, ar_size) ? cout << "Yes" : cout << "No" ; return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to check if there exist // a twin pythagorean triplet in // the given array static boolean isTriplet( int ar[], int n) { // Loop to check if there is a // Pythagorean triplet in the array for ( int i = 0 ; i < n; i++) { for ( int j = i + 1 ; j < n; j++) { for ( int k = j + 1 ; k < n; k++) { // Check if there is // consecutive triple if (Math.abs(ar[i] - ar[j]) == 1 || Math.abs(ar[j] - ar[k]) == 1 || Math.abs(ar[i] - ar[k]) == 1 ) { // Calculate square of // array elements int x = ar[i] * ar[i], y = ar[j] * ar[j], z = ar[k] * ar[k]; if (x == y + z || y == x + z || z == x + y) return true ; } } } } return false ; } // Driver Code public static void main(String[] args) { // Given array arr[] int arr[] = { 3 , 1 , 4 , 6 , 5 }; int ar_size = arr.length; // Function call if (isTriplet(arr, ar_size)) System.out.print( "Yes" ); else System.out.print( "No" ); } } // This code is contributed by gauravrajput1 |
Python3
# Python3 program for the above approach # Function to check if there exist # a twin pythagorean triplet in # the given array def isTriplet(ar, n): # Loop to check if there is a # Pythagorean triplet in the array for i in range (n): for j in range (i + 1 , n): for k in range (j + 1 , n): # Check if there is # consecutive triple if ( abs (ar[i] - ar[j]) = = 1 or abs (ar[j] - ar[k]) = = 1 or abs (ar[i] - ar[k]) = = 1 ): # Calculate square of # array elements x = ar[i] * ar[i] y = ar[j] * ar[j] z = ar[k] * ar[k] if (x = = y + z or y = = x + z or z = = x + y): return True return False # Driver Code if __name__ = = '__main__' : # Given array arr[] arr = [ 3 , 1 , 4 , 6 , 5 ] ar_size = len (arr) # Function call if isTriplet(arr, ar_size): print ( 'Yes' ) else : print ( 'No' ) # This code is contributed by rutvik_56 |
C#
// C# program for the above approach using System; class GFG{ // Function to check if there exist // a twin pythagorean triplet in // the given array static bool isTriplet( int []ar, int n) { // Loop to check if there is a // Pythagorean triplet in the array for ( int i = 0; i < n; i++) { for ( int j = i + 1; j < n; j++) { for ( int k = j + 1; k < n; k++) { // Check if there is // consecutive triple if (Math.Abs(ar[i] - ar[j]) == 1 || Math.Abs(ar[j] - ar[k]) == 1 || Math.Abs(ar[i] - ar[k]) == 1) { // Calculate square of // array elements int x = ar[i] * ar[i], y = ar[j] * ar[j], z = ar[k] * ar[k]; if (x == y + z || y == x + z || z == x + y) return true ; } } } } return false ; } // Driver Code public static void Main(String[] args) { // Given array []arr int []arr = { 3, 1, 4, 6, 5 }; int ar_size = arr.Length; // Function call if (isTriplet(arr, ar_size)) Console.Write( "Yes" ); else Console.Write( "No" ); } } // This code is contributed by Rohit_ranjan |
Javascript
<script> // Javascript program for the above approach // Function to check if there exist // a twin pythagorean triplet in // the given array function isTriplet(ar, n) { // Loop to check if there is a // Pythagorean triplet in the array for ( var i = 0; i < n; i++) { for ( var j = i + 1; j < n; j++) { for ( var k = j + 1; k < n; k++) { // Check if there is // consecutive triple if (Math.abs(ar[i] - ar[j]) == 1 || Math.abs(ar[j] - ar[k]) == 1 || Math.abs(ar[i] - ar[k]) == 1) { // Calculate square of // array elements var x = ar[i] * ar[i], y = ar[j] * ar[j], z = ar[k] * ar[k]; if (x == y + z || y == x + z || z == x + y) return true ; } } } } return false ; } // Driver Code // Given array arr[] var arr = [ 3, 1, 4, 6, 5 ]; var ar_size = arr.length; // Function Call isTriplet(arr, ar_size) ? document.write( "Yes" ) : document.write( "No" ); // This code is contributed by itsok. </script> |
Yes
Time Complexity: O(N3)
Auxiliary Village: O(1)
Efficient Approach:
- Do square of every element in input array. This step takes O(n) time.
- Sort the squared array in increasing order. This step takes O(nLogn) time.
- To find a triplet (a, b, c) such that a2 = b2 + c2 and atleast a pair is consecutive, do following.
- Fix ‘a’ as last element of sorted array.
- Now search for pair (b, c) in subarray between first element and ‘a’. A pair (b, c) with given sum can be found in O(n) time using meet in middle algorithm discussed in method 1 of this post.
- If no pair found for current ‘a’, then move ‘a’ one position back and repeat the previous step.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if any of the // two numbers are consecutive // in the given triplet bool isConsecutive( int a, int b, int c) { return abs (a - b) == 1 || abs (b - c) == 1 || abs (c - a) == 1; } // Function to check if there exist a // pythagorean triplet in the array bool isTriplet( int arr[], int n) { // Square array elements for ( int i = 0; i < n; i++) arr[i] = arr[i] * arr[i]; // Sort array elements sort(arr, arr + n); // Now fix one element one by one // and find the other two elements for ( int i = n - 1; i >= 2; i--) { // To find the other two elements, // start two index variables from // two corners of the array and move // them toward each other int l = 0; int r = i - 1; while (l < r) { // A triplet found if (arr[l] + arr[r] == arr[i] && isConsecutive( sqrt (arr[l]), sqrt (arr[r]), sqrt (arr[i]))) { return true ; } // Else either move 'l' or 'r' (arr[l] + arr[r] < arr[i]) ? l++ : r--; } } // If we reach here, // then no triplet found return false ; } // Driver Code int main() { // Given array arr[] int arr[] = { 3, 1, 4, 6, 5 }; int arr_size = sizeof (arr) / sizeof (arr[0]); // Function Call isTriplet(arr, arr_size) ? cout << "Yes" : cout << "No" ; return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to check if any of the // two numbers are consecutive // in the given triplet static boolean isConsecutive( int a, int b, int c) { return Math.abs(a - b) == 1 || Math.abs(b - c) == 1 || Math.abs(c - a) == 1 ; } // Function to check if there exist a // pythagorean triplet in the array static boolean isTriplet( int arr[], int n) { // Square array elements for ( int i = 0 ; i < n; i++) arr[i] = arr[i] * arr[i]; // Sort array elements Arrays.sort(arr); // Now fix one element one by one // and find the other two elements for ( int i = n - 1 ; i >= 2 ; i--) { // To find the other two elements, // start two index variables from // two corners of the array and move // them toward each other int l = 0 ; int r = i - 1 ; while (l < r) { // A triplet found if (arr[l] + arr[r] == arr[i] && isConsective(( int )Math.sqrt(arr[l]), ( int )Math.sqrt(arr[r]), ( int )Math.sqrt(arr[i]))) { return true ; } // Else either move 'l' or 'r' if (arr[l] + arr[r] < arr[i]) l++; else r--; } } // If we reach here, // then no triplet found return false ; } // Driver Code public static void main(String[] args) { // Given array arr[] int arr[] = { 3 , 1 , 4 , 6 , 5 }; int arr_size = arr.length; // Function call if (isTriplet(arr, arr_size)) System.out.print( "Yes" ); else System.out.print( "No" ); } } // This code is contributed by gauravrajput1 |
Python3
# Python3 program for the above approach import math # Function to check if any of the # two numbers are consecutive # in the given triplet def isConsecutive(a, b, c): return bool ( abs (a - b) = = 1 or abs (b - c) = = 1 or abs (c - a) = = 1 ) # Function to check if there exist a # pythagorean triplet in the array def isTriplet(arr, n): # Square array elements for i in range (n): arr[i] = arr[i] * arr[i] # Sort array elements arr.sort() # Now fix one element one by one # and find the other two elements for i in range (n - 1 , 1 , - 1 ): # To find the other two elements, # start two index variables from # two corners of the array and move # them toward each other l = 0 r = i - 1 while (l < r): # A triplet found if (arr[l] + arr[r] = = arr[i] and isConsective(math.sqrt(arr[l]), math.sqrt(arr[r]), math.sqrt(arr[i]))): return bool ( True ) # Else either move 'l' or 'r' if (arr[l] + arr[r] < arr[i]): l = l + 1 else : r = r - 1 # If we reach here, # then no triplet found return bool ( False ) # Driver code # Given array arr[] arr = [ 3 , 1 , 4 , 6 , 5 ] arr_size = len (arr) # Function call if (isTriplet(arr, arr_size)): print ( "Yes" ) else : print ( "No" ) # This code is contributed divyeshrabadiya07 |
C#
// C# program for the above approach using System; class GFG{ // Function to check if any of the // two numbers are consecutive // in the given triplet static bool isConsecutive( int a, int b, int c) { return Math.Abs(a - b) == 1 || Math.Abs(b - c) == 1 || Math.Abs(c - a) == 1; } // Function to check if there exist a // pythagorean triplet in the array static bool isTriplet( int []arr, int n) { // Square array elements for ( int i = 0; i < n; i++) arr[i] = arr[i] * arr[i]; // Sort array elements Array.Sort(arr); // Now fix one element one by one // and find the other two elements for ( int i = n - 1; i >= 2; i--) { // To find the other two elements, // start two index variables from // two corners of the array and move // them toward each other int l = 0; int r = i - 1; while (l < r) { // A triplet found if (arr[l] + arr[r] == arr[i] && isConsective(( int )Math.Sqrt(arr[l]), ( int )Math.Sqrt(arr[r]), ( int )Math.Sqrt(arr[i]))) { return true ; } // Else either move 'l' or 'r' if (arr[l] + arr[r] < arr[i]) l++; else r--; } } // If we reach here, // then no triplet found return false ; } // Driver Code public static void Main(String[] args) { // Given array []arr int []arr = { 3, 1, 4, 6, 5 }; int arr_size = arr.Length; // Function call if (isTriplet(arr, arr_size)) Console.Write( "Yes" ); else Console.Write( "No" ); } } // This code is contributed by gauravrajput1 |
Javascript
<script> // JavaScript program for the above approach // Function to check if any of the // two numbers are consecutive // in the given triplet function isConsecutive( a, b, c){ return Math.abs(a - b) == 1 || Math.abs(b - c) == 1 || Math.abs(c - a) == 1; } // Function to check if there exist a // pythagorean triplet in the array function isTriplet( arr, n){ // Square array elements for (let i = 0; i < n; i++) arr[i] = arr[i] * arr[i]; // Sort array elements arr.sort( function (a, b){ return a - b}); // Now fix one element one by one // and find the other two elements for (let i = n - 1; i >= 2; i--) { // To find the other two elements, // start two index variables from // two corners of the array and move // them toward each other let l = 0; let r = i - 1; while (l < r) { // A triplet found if (arr[l] + arr[r] == arr[i] && isConsecutive(Math.sqrt(arr[l]), Math.sqrt(arr[r]), Math.sqrt(arr[i]))) { return true ; } // Else either move 'l' or 'r' (arr[l] + arr[r] < arr[i]) ? l++ : r--; } } // If we reach here, // then no triplet found return false ; } // Driver Code // Given array arr[] let arr = [ 3, 1, 4, 6, 5 ]; let arr_size = arr.length; // Function Call isTriplet(arr, arr_size) ?document.write( "Yes" ) : document.write( "No" ); </script> |
Yes
Time Complexity: O(N2)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!