Given three integers a, b and c as triplets. Check if it is possible to make right angled triangle or not. Print Yes if possible, else No. 10-18 <= a, b, c <= 1018
Input: 3 4 5 Output: Yes Explanation: Since 3*3 + 4*4 = 5*5 Hence print "Yes" Input: 8 5 13 Since 8 + 5 < 13 which violates the property of triangle. Hence print "No"
For a right angled triangle to be valid it must satisfies the following criteria:-
- a, b and c should be greater then 0.
- Sum of any two sides of triangle must be greater than the third side.
- Pythagorean Theorem i.e., a2 + b2 = c2.
First two conditions can be easily checked but for third condition we have to take care of overflow. Since a, b and c can be large so we can’t compare them directly unless we use python or BigInteger library in Java. For languages like C and C++, we have to reduce the expression in fraction form.
Before comparing the fraction we need convert them in simplified form by dividing the numerator and denominator by gcd of both of them. Now compare both numerator and denominator of both the fractions of LHS and RHS such that if both would become same then it signifies the valid right angled triangle otherwise not.
Output: Yes No Yes
Time complexity: O(log(M)) where M is the Maximum value among a, b and c.
Auxiliary space: O(1)
This article is contributed by Shubham Bansal. 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.
- Check whether triangle is valid or not if sides are given
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Find the dimensions of Right angled triangle
- Number of possible pairs of Hypotenuse and Area to form right angled triangle
- Find other two sides of a right angle triangle
- Find other two sides and angles of a right angle triangle
- Find area of triangle if two vectors of two adjacent sides are given
- Probability of cutting a rope into three pieces such that the sides form a triangle
- Minimum increment in the sides required to get non-negative area of a triangle
- Check if URL is valid or not in Java
- Check if the game is valid or not
- Check if the given chessboard is valid or not
- Check if given four integers (or sides) make rectangle
- Check if the given permutation is a valid DFS of graph
- Check if any valid sequence is divisible by M
Improved By : Shubham Bansal 13