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.
Examples:
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
![]()
C++
// C++ program to find the Pythagorean// Triplet with given sum#include <bits/stdc++.h>using namespace std;// Function to calculate the// Pythagorean triplet in O(n)void PythagoreanTriplet(int n){ int flag = 0; // Iterate a from 1 to N-1. for (int a = 1; a < n; a++) { // Calculate value of b int b = (n * n - 2 * n * a) / (2 * n - 2 * a); // The value of c = n - a - b int c = n - a - b; if (a * a + b * b == c * c && b > 0 && c > 0) { cout << a << " " << b << " " << c; flag = 1; break; } } if (flag == 0) { cout << "-1"; } return;}// Driver Codeint main(){ int N = 12; // Function call PythagoreanTriplet(N); return 0;} |
Java
// Java program to find the Pythagorean// Triplet with given sumclass GFG { // Function to calculate the // Pythagorean triplet in O(n) static void PythagoreanTriplet(int n) { int flag = 0; // Iterate a from 1 to N-1. for (int a = 1; a < n; a++) { // Calculate value of b int b = (n * n - 2 * n * a) / (2 * n - 2 * a); // The value of c = n - a - b int c = n - a - b; if (a * a + b * b == c * c && b > 0 && c > 0) { System.out .print(a + " " + b + " " + c); flag = 1; break; } } if (flag == 0) { System.out.print("-1"); } return; } // Driver Code public static void main(String[] args) { int N = 12; // Function call PythagoreanTriplet(N); }}// This code contributed by sapnasingh4991 |
Python3
# Python3 program to find the Pythagorean# Triplet with a given sum# Function to calculate the# Pythagorean triplet in O(n)def PythagoreanTriplet(n): flag = 0 # Iterate a from 1 to N-1. for a in range(1, n, 1): # Calculate value of b b = (n * n - 2 * n * a) // (2 * n - 2 * a) # The value of c = n - a - b c = n - a - b if (a * a + b * b == c * c and b > 0 and c > 0): print(a, b, c) flag = 1 break if(flag == 0): print("-1") return# Driver codeif __name__ == '__main__': N = 12 # Function call PythagoreanTriplet(N)# This code is contributed by Bhupendra_Singh |
C#
// C# program to find the Pythagorean// Triplet with given sumusing System;class GFG { // Function to calculate the // Pythagorean triplet in O(n) static void PythagoreanTriplet(int n) { int flag = 0; // Iterate a from 1 to N-1. for (int a = 1; a < n; a++) { // Calculate value of b int b = (n * n - 2 * n * a) / (2 * n - 2 * a); // The value of c = n - a - b int c = n - a - b; if (a * a + b * b == c * c && b > 0 && c > 0) { Console.Write(a + " " + b + " " + c); flag = 1; break; } } if (flag == 0) { Console.Write("-1"); } return; } // Driver code public static void Main(String[] args) { int N = 12; // Function call PythagoreanTriplet(N); }}// This code is contributed by shivanisinghss2110 |
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.
Recommended Posts:
- Pythagorean Triplet with given sum
- Possible pairs forming a Pythagorean Triple with a given value
- Generate a pythagoras triplet from a single integer
- Generate Pythagorean Triplets
- Check if a number is a Pythagorean Prime or not
- Twin Pythagorean triplets in an array
- Minimum possible sum of prices of a Triplet from the given Array
- Find a triplet in an array whose sum is closest to a given number
- Sum of all possible triplet products from given ranges
- 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]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

