Pythagorean Triplet in an Array in C
Pythagorean Triplet in C
A Pythagorean triple consists of three positive integers a, b, and c, such that a² + b² = c². Such a triple is commonly written, and a well-known example is. If is a Pythagorean triple, then so is for any positive integer k. A primitive Pythagorean triple is one in which a, b and c are co-prime.
In this page we will look into a program to find Pythagorean Triplet in an Array in C .
Pythagorean Triplet in an Array in C
We have to find the set of Pythagorean triples from the given array that satisfies pythagorean triplet condition . On this page we will discuss two methods to solve this problem.
- Brute Force (Time complexity: O(n^3) )
- Using Sorting (Time complexity: O(n^2) )
Note
Pythagorean triplets condition: a^2 + b^2 = c^2
Algorithm For Brute Force Method
- We use 3 loops such that we take set of 3 different elements from the array.
- We run 3 for loops. Such that for every a we take all the values b except itself. For every b we take all the values of a.
- a, b, c are elements from the array.
- we run the Pythagorean condition that is a*a + b*b = c*c, for all the sets of (a, b, c). we print them when it is true.
- If a^2 + b^2 = c^2, print a, b, c.
Method 1 :Brute Force
Run
#include <stdio.h> int isTriplet(int arr[], int n) { int x, y, z; 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 x = arr[i] * arr[i], y = arr[j] * arr[j], z = arr[k] * arr[k]; if (x == y + z || y == x + z || z == x + y) { printf("%d, %d, %d",arr[i],arr[j],arr[k]); return 1; } } } } // If we reach here, no triplet found return -1; } /* Driver program to test above function */ int main() { int arr[]={3,4,6,5,7,8}; int n = sizeof(arr) / sizeof(arr[0]); int result = isTriplet(arr,n); if(result == -1) { printf("no triples found"); } return 0; }
Output
3, 4, 5
Algorithm For Sorting Method
- Sort the given array in ascending order.
- Iterate over the array and for each element, find its square.
- Initialize two pointers, left and right, at positions 0 and n-1 respectively, where n is the size of the array.
- While left < right, check if the sum of squares of elements at left and right pointers is equal to the square of the current element. If so, we have found a Pythagorean triplet and we can return true. If the sum is less than the square, move the left pointer to the right. If the sum is greater than the square, move the right pointer to the left.
- If we exhaust all possible combinations of left and right pointers and do not find a Pythagorean triplet, return false.
Method 2 : Using Sorting
Run
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <math.h> int cmpfunc (const void *a, const void *b) { return (*(int *) a - *(int *) b); } void isTriplet (int arr[], int n) { // Step 1 for (int i = 0; i < n; i++) arr[i] = arr[i] * arr[i]; // Sort array elements qsort (arr, n, sizeof (int), cmpfunc); // Step 2 and Step 3 for (int i = n - 1; i >= 2; i--) { // Fixing a int b = 0; // Fixing b int c = i - 1; // Fixing c while (b < c) { // A triplet found if (arr[b] + arr[c] == arr[i]) { int x = sqrt (arr[b]); int y = sqrt (arr[c]); int z = sqrt (arr[i]); printf ("%d, %d, %d \n", x, y, z); b++; c--; } // Else either move 'b' or 'c' else if (arr[b] + arr[c] < arr[i]) { b++; } else { c--; } } } } // Driver code int main () { int arr[] = { 3, 4, 6, 5, 7, 8 }; int n = sizeof (arr) / sizeof (arr[0]); isTriplet (arr, n); return 0; }
Output
3, 4, 5
Login/Signup to comment