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, 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++
#include <bits/stdc++.h>
using namespace std;
bool isTriplet( int ar[], int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = j + 1; k < n; k++) {
if ( abs (ar[i] - ar[j]) == 1
|| abs (ar[j] - ar[k]) == 1
|| abs (ar[i] - ar[k]) == 1) {
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 ;
}
int main()
{
int arr[] = { 3, 1, 4, 6, 5 };
int ar_size = sizeof (arr) / sizeof (arr[0]);
isTriplet(arr, ar_size)
? cout << "Yes"
: cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isTriplet( int ar[], int n)
{
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
for ( int k = j + 1 ; k < n; k++)
{
if (Math.abs(ar[i] - ar[j]) == 1 ||
Math.abs(ar[j] - ar[k]) == 1 ||
Math.abs(ar[i] - ar[k]) == 1 )
{
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 ;
}
public static void main(String[] args)
{
int arr[] = { 3 , 1 , 4 , 6 , 5 };
int ar_size = arr.length;
if (isTriplet(arr, ar_size))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isTriplet(ar, n):
for i in range (n):
for j in range (i + 1 , n):
for k in range (j + 1 , n):
if ( abs (ar[i] - ar[j]) = = 1 or
abs (ar[j] - ar[k]) = = 1 or
abs (ar[i] - ar[k]) = = 1 ):
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
if __name__ = = '__main__' :
arr = [ 3 , 1 , 4 , 6 , 5 ]
ar_size = len (arr)
if isTriplet(arr, ar_size):
print ( 'Yes' )
else :
print ( 'No' )
|
C#
using System;
class GFG{
static bool isTriplet( int []ar, int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
for ( int k = j + 1; k < n; k++)
{
if (Math.Abs(ar[i] - ar[j]) == 1 ||
Math.Abs(ar[j] - ar[k]) == 1 ||
Math.Abs(ar[i] - ar[k]) == 1)
{
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 ;
}
public static void Main(String[] args)
{
int []arr = { 3, 1, 4, 6, 5 };
int ar_size = arr.Length;
if (isTriplet(arr, ar_size))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isTriplet(ar, n)
{
for ( var i = 0; i < n; i++) {
for ( var j = i + 1; j < n; j++) {
for ( var k = j + 1; k < n; k++) {
if (Math.abs(ar[i] - ar[j]) == 1
|| Math.abs(ar[j] - ar[k]) == 1
|| Math.abs(ar[i] - ar[k]) == 1) {
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 ;
}
var arr = [ 3, 1, 4, 6, 5 ];
var ar_size = arr.length;
isTriplet(arr, ar_size)
? document.write( "Yes" )
: document.write( "No" );
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
bool isConsecutive( int a, int b, int c)
{
return abs (a - b) == 1
|| abs (b - c) == 1
|| abs (c - a) == 1;
}
bool isTriplet( int arr[], int n)
{
for ( int i = 0; i < n; i++)
arr[i] = arr[i] * arr[i];
sort(arr, arr + n);
for ( int i = n - 1; i >= 2; i--) {
int l = 0;
int r = i - 1;
while (l < r) {
if (arr[l] + arr[r] == arr[i]
&& isConsecutive( sqrt (arr[l]),
sqrt (arr[r]),
sqrt (arr[i]))) {
return true ;
}
(arr[l] + arr[r] < arr[i])
? l++
: r--;
}
}
return false ;
}
int main()
{
int arr[] = { 3, 1, 4, 6, 5 };
int arr_size = sizeof (arr)
/ sizeof (arr[0]);
isTriplet(arr, arr_size)
? cout << "Yes"
: cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
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 ;
}
static boolean isTriplet( int arr[], int n)
{
for ( int i = 0 ; i < n; i++)
arr[i] = arr[i] * arr[i];
Arrays.sort(arr);
for ( int i = n - 1 ; i >= 2 ; i--)
{
int l = 0 ;
int r = i - 1 ;
while (l < r)
{
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 ;
}
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 };
int arr_size = arr.length;
if (isTriplet(arr, arr_size))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
import math
def isConsecutive(a, b, c):
return bool ( abs (a - b) = = 1 or
abs (b - c) = = 1 or
abs (c - a) = = 1 )
def isTriplet(arr, n):
for i in range (n):
arr[i] = arr[i] * arr[i]
arr.sort()
for i in range (n - 1 , 1 , - 1 ):
l = 0
r = i - 1
while (l < r):
if (arr[l] + arr[r] = = arr[i] and
isConsective(math.sqrt(arr[l]),
math.sqrt(arr[r]),
math.sqrt(arr[i]))):
return bool ( True )
if (arr[l] + arr[r] < arr[i]):
l = l + 1
else :
r = r - 1
return bool ( False )
arr = [ 3 , 1 , 4 , 6 , 5 ]
arr_size = len (arr)
if (isTriplet(arr, arr_size)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
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;
}
static bool isTriplet( int []arr, int n)
{
for ( int i = 0; i < n; i++)
arr[i] = arr[i] * arr[i];
Array.Sort(arr);
for ( int i = n - 1; i >= 2; i--)
{
int l = 0;
int r = i - 1;
while (l < r)
{
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 ;
}
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 };
int arr_size = arr.Length;
if (isTriplet(arr, arr_size))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isConsecutive( a, b, c){
return Math.abs(a - b) == 1
|| Math.abs(b - c) == 1
|| Math.abs(c - a) == 1;
}
function isTriplet( arr, n){
for (let i = 0; i < n; i++)
arr[i] = arr[i] * arr[i];
arr.sort( function (a, b){ return a - b});
for (let i = n - 1; i >= 2; i--) {
let l = 0;
let r = i - 1;
while (l < r) {
if (arr[l] + arr[r] == arr[i]
&& isConsecutive(Math.sqrt(arr[l]),
Math.sqrt(arr[r]),
Math.sqrt(arr[i]))) {
return true ;
}
(arr[l] + arr[r] < arr[i])
? l++
: r--;
}
}
return false ;
}
let arr = [ 3, 1, 4, 6, 5 ];
let arr_size = arr.length;
isTriplet(arr, arr_size)
?document.write( "Yes" )
: document.write( "No" );
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
17 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...