
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the Number of Possible Pairs of Hypotenuse and Area to Form Right Angled Triangle using C++
In this article, we will explain how to solve the number of possible pairs of hypotenuse and area form a right-angled triangle in C++.
We need to determine the number of all possible pairs of a hypotenuse and the area ( H, A ) to form a right-angled triangle with H as hypotenuse and A as Area.
In this example −
x = Base of Right Angled Triangle
y = Height of Right Angled Triangle
H = hypotenuse of Right Angled Triangle
We know Area of right angled triangle,
A = ( x * y ) / 2
Or
4 * A2 = ( x * y )2 …… (1)
Also we know
x2 + y2 =H2 …… (2)
Solving (1) & (2)
4 * A2 = x2 ( H2 - x2 )
Solving the quadratic equation in x2 and puting D (discriminant) >= 0 ( for x to exist)
We get, H2 >= 4 * A (condition for right-angle triangle to exist )
Here is the example −
Input : array H[ ] = { 3, 6, 8 } : A[ ] = { 2, 31, 12 } Output : 4 Explanation : possible pairs of Hypotenuse and Area ( H, A ) are ( 3, 2 ), ( 6, 2 ), ( 8, 2 ) and ( 8, 12 ). Input : array H[ ] = { 2, 5, 9 } : A[ ] = { 3, 11, 7 } Output : 4 Explanation : possible pairs of Hypotenuse and Area ( H, A ) are possible pairs of Hypotenuse and Area ( H, A ) are ( 5, 3 ), ( 9, 3 ), ( 9, 11 ) and ( 9, 7 ).
Approach to Find Solutions
Now we will use two different methods to perform the given task −
Brute Force Approach
In this simple approach, we find all the possible pairs of hypotenuse and area ( H, A ), check whether they satisfy the condition, h2 >= 4 * A or not, and count for every pair found which satisfies this condition.
Example
#include <iostream> using namespace std; int main(){ int H[ ] = { 2,5,9}; // array of hypotenuse int s1 = sizeof(H)/sizeof(H[0]); int A[ ] = { 3, 11, 7};// array of area int s2 = sizeof(A)/sizeof(A[0]); int count = 0;// initialising count to 0 // finding all possible pairs for (int i = 0; i < s1; i++) { for (int j = 0; j < s2; j++) { // checking whether current pair satisfies the condition if (H[i] * H[i] >= 4 * A[j]){ count++; } } } cout << "Number of possible pairs of ( H, A ): " << count ; return 0; }
Output
Number of possible pairs of ( H, A ): 4
Explanation
In this code, we use count variables to keep the count of pairs satisfying the equation and using nested loops to generate ( H, A ) pairs. The time complexity of this code is O(n2) which is not an efficient approach. Let's understand the second approach.
Efficient approach
In this approach, we are first sorting both the arrays in ascending order, then we are finding any hypotenuse length to find the maximum area on checking H2 > 4 * A.
Example
#include <bits/stdc++.h> using namespace std; int main (){ int H[] = { 2, 5, 9 }; int s1 = sizeof (H) / sizeof (H[0]); int A[] = { 3, 11, 7 }; int s2 = sizeof (A) / sizeof (A[0]); int count = 0; // Sorting both the arrays sort (H, H + s1); sort (A, A + s2); int temp = -1; for (int i = 0; i < s1; i++){ // Applying binary search for // every Hypotenuse Length int flag1 = 0; int flag2 = s2 - 1; while (flag1 <= flag2){ int mid = flag1 + (flag2 - flag1) / 2; if ((H[i] * H[i]) >= (4 * A[mid])){ temp = mid; flag1 = mid + 1; } else{ flag2 = mid - 1; } } if (temp != -1){// Check if we get any possible area count += temp + 1; } } cout << "Number of possible pairs of (H, A): " << count; return 0; }
Output
Number of possible pairs of ( H, A ): 4
Explanation of the Above Code
In this code, we are first sorting both the arrays in ascending order, and then we are checking for every possible length using binary search in order to find the maximum area.
Let's say the maximum area is found at index 3 in the array of area A[ ], then all areas less than index three will also satisfy the equation so that we can form 3 possible pairs.
Conclusion
In this article, we solved a problem to find the number of Hypotenuse and Area pairs that would be used to make a right-angled triangle. We applied the Brute force approach, whose Time complexity was O(n2), and the Efficient approach, whose Time complexity was O(s1 log(s2)). Hope you find this article helpful.
- Related Articles
- The length of the hypotenuse of an isosceles right-angled triangle is 24. Find the area of the triangle.
- The length of the hypotenuse of an isosceles right-angled triangle is \( 20 . \) Find the perimeter and area of the triangle.
- Swift Program to find the hypotenuse of a right-angled triangle with sides l and b
- Check if right triangle possible from given area and hypotenuse in Python
- Find the hypotenuse of a right angled triangle with given two sides in C++
- Area of Circumcircle of a Right Angled Triangle?
- Prove that $(2, -2), (-2, 1)$ and $(5, 2)$ are the vertices of a right angled triangle. Find the area of the triangle and the length of the hypotenuse.
- In A Right Angled Triangle Base =12cm And Hypotenuse =15 CM Find The Perpendicular
- The two sides of a right-angled triangle are 6 cm and 8 cm. Find the length of the hypotenuse.
- In a right - angled triangle, base is $12\ cm$ and hypotenuse is $15\ cm$. Find the Perpendicular.
- Area of Circumcircle of a Right Angled Triangle in C Program?
- Area of Incircle of a Right Angled Triangle in C Program?
- Show that in a right angled triangle, the hypotenuse is the longest side.
- Find the dimensions of Right angled triangle in C++
- The hypotenuse of a right angled triangle is \( 5 \mathrm{~cm} \) and the other two sides differ by \( 1 \mathrm{~cm} \). Find the other two sided of the triangle.
