
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Possible Pairs of Hypotenuse and Area for 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.