Given area and hypotenuse, the aim is to print all sides if right triangle can exist, else print -1. We need to print all sides in ascending order.
Examples:
Input : 6 5
Output : 3 4 5
Input : 10 6
Output : -1
We have discussed a solution of this problem in below post.
Find all sides of a right angled triangle from given hypotenuse and area | Set 1
In this post, a new solution with below logic is discussed.
Let the two unknown sides be a and b
Area : A = 0.5 * a * b
Hypotenuse Square : H^2 = a^2 + b^2
Substituting b, we get H2 = a2 + (4 * A2)/a2
On re-arranging, we get the equation a4 – (H2)(a2) + 4*(A2)
The discriminant D of this equation would be D = H4 – 16*(A2)
If D = 0, then roots are given by the linear equation formula, roots = (-b +- sqrt(D) )/2*a
these roots would be equal to the square of the sides, finding the square roots would give us the sides.
C++
#include <bits/stdc++.h>
using namespace std;
void findRightAngle( int A, int H)
{
long D = pow (H, 4) - 16 * A * A;
if (D >= 0)
{
long root1 = (H * H + sqrt (D)) / 2;
long root2 = (H * H - sqrt (D)) / 2;
long a = sqrt (root1);
long b = sqrt (root2);
if (b >= a)
cout << a << " " << b << " " << H;
else
cout << b << " " << a << " " << H;
}
else
cout << "-1" ;
}
int main()
{
findRightAngle(6, 5);
}
|
Java
class GFG {
static void findRightAngle( double A, double H)
{
double D = Math.pow(H, 4 ) - 16 * A * A;
if (D >= 0 )
{
double root1 = (H * H + Math.sqrt(D)) / 2 ;
double root2 = (H * H - Math.sqrt(D)) / 2 ;
double a = Math.sqrt(root1);
double b = Math.sqrt(root2);
if (b >= a)
System.out.print(a + " " + b + " " + H);
else
System.out.print(b + " " + a + " " + H);
}
else
System.out.print( "-1" );
}
public static void main(String arg[])
{
findRightAngle( 6 , 5 );
}
}
|
Python3
from math import sqrt
def findRightAngle(A, H):
D = pow (H, 4 ) - 16 * A * A
if D > = 0 :
root1 = (H * H + sqrt(D)) / / 2
root2 = (H * H - sqrt(D)) / / 2
a = int (sqrt(root1))
b = int (sqrt(root2))
if b > = a:
print (a, b, H)
else :
print (b, a, H)
else :
print ( "-1" )
findRightAngle( 6 , 5 )
|
C#
using System;
class GFG {
static void findRightAngle( double A, double H)
{
double D = Math.Pow(H, 4) - 16 * A * A;
if (D >= 0) {
double root1 = (H * H + Math.Sqrt(D)) / 2;
double root2 = (H * H - Math.Sqrt(D)) / 2;
double a = Math.Sqrt(root1);
double b = Math.Sqrt(root2);
if (b >= a)
Console.WriteLine(a + " " + b + " " + H);
else
Console.WriteLine(b + " " + a + " " + H);
}
else
Console.WriteLine( "-1" );
}
public static void Main()
{
findRightAngle(6, 5);
}
}
|
PHP
<?php
function findRightAngle( $A , $H )
{
$D = pow( $H , 4) - 16 * $A * $A ;
if ( $D >= 0)
{
$root1 = ( $H * $H + sqrt( $D )) / 2;
$root2 = ( $H * $H - sqrt( $D )) / 2;
$a = sqrt( $root1 );
$b = sqrt( $root2 );
if ( $b >= $a )
echo $a , " " , $b , " " , $H ;
else
echo $b , " " , $a , " " , $H ;
}
else
echo "-1" ;
}
findRightAngle(6, 5);
?>
|
Javascript
<script>
function findRightAngle(A,H)
{
let D = Math.pow(H, 4) - 16 * A * A;
if (D >= 0)
{
let root1 = (H * H + Math.sqrt(D)) / 2;
let root2 = (H * H - Math.sqrt(D)) / 2;
let a = Math.sqrt(root1);
let b = Math.sqrt(root2);
if (b >= a)
document.write(a + " " + b + " " + H+ "<br/>" );
else
document.write(b + " " + a + " " + H+ "<br/>" );
}
else
document.write( "-1" );
}
findRightAngle(6, 5);
</script>
|
Output:
3 4 5
Time complexity: O(log(n)) since using inbuilt sqrt functions
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Aug, 2022
Like Article
Save Article