Find Pythagorean Triplets from array

Find Pythagorean Triplets in C++

Pythagorean triplets are set of three values which satisfy the pythagoras theorem of a right angled triangle. Pythagoras theorem states that the square of hypotenuse is equal to the sum of square of base and square of perpendicular. The values which satisfy the above condition are considered to be as pythagorean triplets.On this page, we will learn about how to find pythagorean triplets from array in C++.

Introduction to stl

Method 1

We use a brute force algorithm here :

Algorithm

Time complexity: O(n^3)

Step 1 :   We use 3 loops such that we take set of 3 different elements from the array.
a. 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.
b. a, b, c are elements from the array.

Step 2 :   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.
a. If a^2 + b^2 = c^2, print a, b, c.

Code

Run
#include <bits/stdc++.h>
using namespace std;

int main ()
{

  int arr[] = { 3, 8, 4, 10, 6, 5, 12, 13, 27 };	
  int N = sizeof (arr) / sizeof (arr[0]);	
  int a, b, c;
  for (int i = 0; i < N - 2; i++)
    {
      for (int j = i + 1; j < N - 1; j++)
	  {
	    for (int k = i + 2; k < N; k++)
	    {
	      a = arr[i];
	      b = arr[j];
	      c = arr[k];
	      if (a * a + b * b == c * c)
		      cout << a << " " << b << " " << c << endl;
	    }
	  }
    }
  return 0;
}

Output

3 4 5
8 6 10
5 12 13

Method 2

 

Step 1 : Sort the given array first using the function sort.
Step 2 : Instead of storing the numbers store the square of each element to directly check the Pythagorean theorem.
Step 3 : Take a as the smallest side, for every a check the elements from the array which satisfy the condition (a = c – b). if they satisfy this condition they form Pythagorean triplet as they satisfy the condition
a2 + b2 = c2
a. for all elements in the array from start, store the first element as “a”.
b. store the last two elements as “b” and “c” respectively.
c. Check the condition “a = c – b”. if true print the sqrts of a, b, c as a set of Pythagorean triplets.
d. If “c – b” is greater than “a”, decrease the variable pointing at the larger element(c) so that we are checking for all “c” is this condition true or not. If “c – b” is less than a decrease the variable pointing at the smaller element so that we are checking for all b is this condition true or not.
e. continue this loop for all a`s
f. If not found any, print no triplets.

Code

Run
#include <bits/stdc++.h>
using namespace std;

int main ()
{

  int arr[] =
    { 3, 8, 4, 10, 6, 5, 12, 13, 27, 117, 165, 19, 176, 169, 44, 113, 24, 145, 
       143, 51, 149, 52, 173, 181, 125 };
  int N = sizeof (arr) / sizeof (arr[0]);

  int a, b, c;
  sort (arr, arr + N);
  for (int i = 0; i < N; i++)
    arr[i] = (arr[i] * arr[i]);
  for (int i = 0; i < N; i++)
  {
      int left = N - 2, right = N - 1;
      a = arr[i];

      while (left > i)
	  {
	    b = arr[left];
	    c = arr[right];

	    int calculated_side = c - b;
	    if (calculated_side == a)
	    {
	      cout << sqrt (a) << " " << sqrt (b) << " " << sqrt (c) << endl;
	      left++;
	      right --;
	    }
	    else if (calculated_side > a)
	      right --;
	    else
	      left --;
	   }
   }
  return 0;
}


Output

3 4 5
5 12 13
6 8 10
24 143 145
44 117 125
52 165 173

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java