Given two positive integers A and B representing the sides of a triangle, the task is to check if the given two sides of the triangle are sides of a valid right-angled triangle or not. If found to be true, print “YES“. Otherwise, print “No”.
Examples:
Input: A = 3, B = 4
Output: Yes
Explanation: A right-angled triangle is possible with side lengths 3, 4 and 5.Input : A = 2, B = 5
Output: No
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say checkTriangle to check if the given two sides of the triangle are sides of right-angled triangle not.
- Check if the value of B2 + A2 is aperfect square or not. If found to be true, then update checkTriangle = True..
- Otherwise, check if the value of B2 – A2 is a perfect square or not. If found to be true, then update checkTriangle = True.
- Otherwise, check if the value of A2 – B2 is a perfect square number or not. If found to be true, then update checkTriangle = True.
- Finally, print “Yes” if checkTriangle is True. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to check if N is a // perfect square number or not int checkPerfectSquare( int N) { // If N is a non // positive integer if (N <= 0) { return 0; } // Stores square root // of N double sq = sqrt (N); // Check for perfect square if ( floor (sq) == ceil (sq)) { return 1; } // If N is not a // perfect square number return 0; } // Function to check if given two sides of a // triangle forms a right-angled triangle bool checktwoSidesareRighTriangle( int A, int B) { bool checkTriangle = false ; // If the value of (A * A + B * B) is a // perfect square number if (checkPerfectSquare(A * A + B * B)) { // Update checkTriangle checkTriangle = true ; } // If the value of (A * A - B * B) is a // perfect square number if (checkPerfectSquare(A * A - B * B)) { // Update checkTriangle checkTriangle = true ; } // If the value of (B * B - A * A) is a // perfect square number if (checkPerfectSquare(B * B - A * A)) { // Update checkTriangle checkTriangle = true ; } return checkTriangle; } // Driver Code int main() { int A = 3, B = 4; // If the given two sides of a triangle // forms a right-angled triangle if (checktwoSidesareRighTriangle(A, B)) { cout << "Yes" ; } // Otherwise else { cout << "No" ; } return 0; } |
Java
// Java program to implement // the above approach import java.io.*; import java.util.*; class GFG{ // Function to check if N is a // perfect square number or not static int checkPerfectSquare( int N) { // If N is a non // positive integer if (N <= 0 ) { return 0 ; } // Stores square root // of N double sq = Math.sqrt(N); // Check for perfect square if (Math.floor(sq) == Math.ceil(sq)) { return 1 ; } // If N is not a // perfect square number return 0 ; } // Function to check if given two sides of a // triangle forms a right-angled triangle static boolean checktwoSidesareRighTriangle( int A, int B) { boolean checkTriangle = false ; // If the value of (A * A + B * B) is a // perfect square number if (checkPerfectSquare(A * A + B * B) != 0 ) { // Update checkTriangle checkTriangle = true ; } // If the value of (A * A - B * B) is a // perfect square number if (checkPerfectSquare(A * A - B * B) != 0 ) { // Update checkTriangle checkTriangle = true ; } // If the value of (B * B - A * A) is a // perfect square number if (checkPerfectSquare(B * B - A * A) != 0 ) { // Update checkTriangle checkTriangle = true ; } return checkTriangle; } // Driver Code public static void main(String[] args) { int A = 3 , B = 4 ; // If the given two sides of a triangle // forms a right-angled triangle if (checktwoSidesareRighTriangle(A, B)) { System.out.print( "Yes" ); } // Otherwise else { System.out.print( "No" ); } } } // This code is contributed by susmitakundugoaldanga |
Python3
# Python3 program to implement # the above approach from math import sqrt, floor, ceil # Function to check if N is a # perfect square number or not def checkPerfectSquare(N): # If N is a non # positive integer if (N < = 0 ): return 0 # Stores square root # of N sq = sqrt(N) # Check for perfect square if (floor(sq) = = ceil(sq)): return 1 # If N is not a # perfect square number return 0 # Function to check if given two sides of a # triangle forms a right-angled triangle def checktwoSidesareRighTriangle(A, B): checkTriangle = False # If the value of (A * A + B * B) is a # perfect square number if (checkPerfectSquare(A * A + B * B)): # Update checkTriangle checkTriangle = True # If the value of (A * A - B * B) is a # perfect square number if (checkPerfectSquare(A * A - B * B)): # Update checkTriangle checkTriangle = True # If the value of (B * B - A * A) is a # perfect square number if (checkPerfectSquare(B * B - A * A)): # Update checkTriangle checkTriangle = True return checkTriangle # Driver Code if __name__ = = '__main__' : A = 3 B = 4 # If the given two sides of a triangle # forms a right-angled triangle if (checktwoSidesareRighTriangle(A, B)): print ( "Yes" ) # Otherwise else : print ( "No" ) # This code is contributed by SURENDRA_GANGWAR |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to check if N is a // perfect square number or not static int checkPerfectSquare( int N) { // If N is a non // positive integer if (N <= 0) { return 0; } // Stores square root // of N double sq = Math.Sqrt(N); // Check for perfect square if (Math.Floor(sq) == Math.Ceiling(sq)) { return 1; } // If N is not a // perfect square number return 0; } // Function to check if given two sides of a // triangle forms a right-angled triangle static bool checktwoSidesareRighTriangle( int A, int B) { bool checkTriangle = false ; // If the value of (A * A + B * B) is a // perfect square number if (checkPerfectSquare(A * A + B * B) != 0) { // Update checkTriangle checkTriangle = true ; } // If the value of (A * A - B * B) is a // perfect square number if (checkPerfectSquare(A * A - B * B) != 0) { // Update checkTriangle checkTriangle = true ; } // If the value of (B * B - A * A) is a // perfect square number if (checkPerfectSquare(B * B - A * A) != 0) { // Update checkTriangle checkTriangle = true ; } return checkTriangle; } // Driver Code public static void Main() { int A = 3, B = 4; // If the given two sides of a triangle // forms a right-angled triangle if (checktwoSidesareRighTriangle(A, B)) { Console.Write( "Yes" ); } // Otherwise else { Console.Write( "No" ); } } } // This code is contributed by code_hunt |
Javascript
<script> // Javascript program to implement // the above approach // Function to check if N is a // perfect square number or not function checkPerfectSquare( N) { // If N is a non // positive integer if (N <= 0) { return 0; } // Stores square root // of N let sq = Math.sqrt(N); // Check for perfect square if (Math.floor(sq) == Math.ceil(sq)) { return 1; } // If N is not a // perfect square number return 0; } // Function to check if given two sides of a // triangle forms a right-angled triangle function checktwoSidesareRighTriangle( A , B) { let checkTriangle = false ; // If the value of (A * A + B * B) is a // perfect square number if (checkPerfectSquare(A * A + B * B) != 0) { // Update checkTriangle checkTriangle = true ; } // If the value of (A * A - B * B) is a // perfect square number if (checkPerfectSquare(A * A - B * B) != 0) { // Update checkTriangle checkTriangle = true ; } // If the value of (B * B - A * A) is a // perfect square number if (checkPerfectSquare(B * B - A * A) != 0) { // Update checkTriangle checkTriangle = true ; } return checkTriangle; } // Driver Code let A = 3, B = 4; // If the given two sides of a triangle // forms a right-angled triangle if (checktwoSidesareRighTriangle(A, B)) { document.write( "Yes" ); } // Otherwise else { document.write( "No" ); } // This code is contributed by Rajput-Ji </script> |
Yes
Time Complexity: O(log(max(A, B))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!