A Pythagorean Triplet is a set of natural numbers such that a < b < c, for which
Given a number N, find a Pythagorean Triplet with sum as given N or return -1.
Input: 12 Output: 3 4 5 Explanation: As 32 + 42 = 52 Input: 82 Output: -1
Approach: The idea is to find the value of b and c in terms of a and iterate a from 1 to N. To find the value of b and c in terms of a we have to do following:
We have two equations,
[Tex] a + b + c = N [/Tex]
We will find the value of c in term of a and b Then put this value in equation 1 to solve for b.
From equation 2,
Now, put this value in equation 1.
After solving the above equation we will get,
[Tex]c = N – b – a [/Tex]
Now, iterate a from 1 to N and calculate respectively the value of b and c Then, check if
3 4 5
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Pythagorean Triplet with given sum
- Generate a pythagoras triplet from a single integer
- Possible pairs forming a Pythagorean Triple with a given value
- Generate Pythagorean Triplets
- Check if a number is a Pythagorean Prime or not
- Twin Pythagorean triplets in an array
- Find a triplet in an array whose sum is closest to a given number
- Minimum possible sum of prices of a Triplet from the given Array
- Sum of multiplication of triplet of divisors of a number
- Triplet with no element divisible by 3 and sum N
- Find triplet with minimum sum
- Puzzle | Place numbers 1 to 9 in a Circle such that sum of every triplet in straight line is 15
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Find distinct integers for a triplet with given product
- Largest lexicographic triplet from a given Array that forms a triangle
- Finding sum of digits of a number until sum becomes single digit
- Prime Triplet
- Finding a Non Transitive Coprime Triplet in a Range
- Count triplet of indices (i, j, k) such that XOR of elements between [i, j) equals [j, k]
- Maximum value of expression (arr[i] + arr[j] * arr[k]) formed from a valid Triplet
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.