
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pythagorean Triplet in an Array Using C++
Pythagorean Triples
Pythagorean triples are a set of three distinct numbers a
, b
, and c
which satisfy the condition:
a2 + b2 = c2
These numbers a
, b
, and c
are known as Pythagorean triples. We can say that these three numbers represent the sides of a right-angled triangle. These three sides are known as triplets of the array.
Problem Description
We are given an array of integers, and we have to find if there exist three distinct numbers in the array which form the Pythagorean triplet in C++. Below are examples to demonstrate the problem statement clearly:
Example 1
Input: array = {3, 1, 4, 6, 5}
Output: Yes
Explanation:
The Pythagorean triplet in this array exists as
three distinct numbers of this array satisfy
the condition because
32 + 42 = 52
Example 2
Input: array = {10, 4, 6, 12, 5} Output: No Explanation: In the given array, no such triplet exists that satisfies the condition of Pythagorean tripletsa
,b
, andc
.
Example 3
Input: array = {16, 20, 13, 21, 29}
Output: Yes
Explanation:
The Pythagorean triplet in this array exists as
three distinct numbers of this array satisfy the
condition because 202 + 212 = 292
Checking Pythagorean Triplet Using Brute Force Approach
In the brute force approach, we use three nested loops to check if a Pythagorean triplet exists in an array, i.e., three distinct elements whose sum satisfies the condition of a2 + b2 = c2
.
Steps for Implementation
- We use three nested loops to traverse and pick three distinct elements
a
,b
, andc
. - Now, check the condition of the Pythagorean triplet:
a2 + b2 = c2
. - If such a triplet exists, we return
Yes
. - If no such triplet exists, we return
No
.
Implementation Code
#include<bits/stdc++.h> using namespace std; bool checkPythagoreanTriplet(vector<int>& arr) { int n = arr.size(); for (int i = 0; i < n - 2; i++) { for (int j = i + 1; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { int a = arr[i], b = arr[j], c = arr[k]; if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) { return true; } } } } return false; } int main() { vector<int> arr = {3, 1, 4, 6, 5}; if (checkPythagoreanTriplet(arr)) { cout << "Yes\n"; } else { cout << "No\n"; } return 0; }
Output
Yes
Time Complexity: O(n3) as we are using three nested loops.
Space Complexity: O(1), as we are using only a few variables.
Checking Pythagorean Triplet Using Sorting Approach
In this approach, we first square all the elements in the array and then sort the array. We fix one element as the largest number of triplets in the array and use the two-pointer technique to find the other two numbers.
Steps for Implementation
- Square all the elements of the array.
- Sort the array.
- Fix one element as the largest element of the triplet (e.g.,
c
ina2 + b2 = c2
). - Use two pointers (e.g., left and right) to find the other two numbers of the triplet,
a
andb
. - Check if the condition
a2 + b2 = c2
satisfies; if so, returnYes
, otherwise returnNo
.
Implementation Code
#include<bits/stdc++.h> using namespace std; bool checkPythagoreanTriplet(vector<int>& arr) { int n = arr.size(); // Square each element for (int i = 0; i < n; i++) { arr[i] = arr[i] * arr[i]; } // Sort the array sort(arr.begin(), arr.end()); // Fix one element as the largest for (int k = n - 1; k >= 2; k--) { int left = 0, right = k - 1; // Use two pointers while (left < right) { if (arr[left] + arr[right] == arr[k]) { return true; } else if (arr[left] + arr[right] < arr[k]) { left++; } else { right--; } } } return false; } int main() { vector<int> arr = {3, 1, 4, 6, 5}; if (checkPythagoreanTriplet(arr)) { cout << "Yes\n"; } else { cout << "No\n"; } return 0; }
Output
Yes
Time Complexity: O(n log n) + O(n2), as we are using sorting and the two-pointer approach.
Space Complexity: O(1), constant space.