Given a perimeter P, the task is to find the number of right triangles possible with perimeter equal to p.
Input: P = 12 Output: number of right triangles = 1 The only right angle possible is with sides hypotenuse = 5, perpendicular = 4 and base = 3. Input: p = 840 Output: number of right triangles = 8
So the aim is to find the number of solutions which satisfy equations a + b + c = p and a2 + b2 = c2.
A naive approach is to run two loops for a(1 to p/2) and b(a+1 to p/3) then make c=p-a-b and increase count by one if . This will take time.
An efficient approach can be found by little algebraic manipulation :
Since a + c > b or, p – b > b or, b < p/2. Thus iterating b from 1 to p/2, calculating a and storing only the whole number a would give all solutions for a given p. There are no right triangles are possible for odd p as right angle triangles follow the Pythagoras theorem. Use a list of pairs to store the values of a and band return the count at the end.
Below is the implementation of the above approach.
number of right triangles = 8
Time complexity: O(P)
- Count the number of possible triangles
- Number of triangles after N moves
- Number of triangles that can be formed with given N points
- Number of Isosceles triangles in a binary tree
- Number of triangles formed from a set of points on three lines
- Number of triangles in a plane if no more than two points are collinear
- Number of triangles possible with given lengths of sticks which are powers of 2
- Number of possible Triangles in a Cartesian coordinate system
- Total number of triangles formed when there are H horizontal and V vertical lines
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of ways to divide a number in 4 parts
- Count the number of operations required to reduce the given number
- Count Number of animals in a zoo from given number of head and legs
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.