Flashcards
Pythagorean Triplet
Cards in this deck (1)
- 1Given an integer array find all Pythagorean triplets.static boolean isTriplet(int arr[], int n)
{
// Square array elements
for (int i=0; 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; // index of the first element in arr[0..i-1]
int r = i-1; // index of the last element in arr[0..i-1]
while (l < r)
{
// A triplet found
if (arr[l] + arr[r] == arr[i])
return true;
// Else either move 'l' or 'r'
if (arr[l] + arr[r] < arr[i])
l++;
else
r--;
}
}
Information
- Description
- Flashcards on Pythagorean Triplet, created by Suhas S on 19/03/2018.
- Created by
- Suhas S
- Views
- 1
- Rating