Rectangular or not?#915

Rectangular or not? (Editorial)

A triangle with sides , and is a right triangle if one of its sides is the hypotenuse and the other two are the legs. According to the Pythagorean theorem, the square of the hypotenuse's length equals the sum of the squares of the lengths of the legs. This means that one of the following conditions must hold:

a^2 = b^2 + c^2, b^2 = a^2 + c^2~or~c^2 = a^2 + b^2,

where and can be the sides of the triangle in any order.

Algorithm implementation

Read the lengths of the triangle's sides.

scanf("%d %d %d",&a,&b,&c);

Check whether the triangle is right-angled and print the corresponding result.

if ((a * a + b * b == c * c) ||
    (a * a + c * c == b * b) ||
    (b * b + c * c == a * a))
  puts("YES");
else
  puts("NO");

Java implementation

import java.util.*;

public class Main
{
  public static void main(String[] args)
  {
    Scanner con = new Scanner(System.in);
    int a = con.nextInt();
    int b = con.nextInt();
    int c = con.nextInt();
    
    if ((a * a + b * b == c * c) ||
        (a * a + c * c == b * b) ||
        (b * b + c * c == a * a))
      System.out.println("YES");
    else
      System.out.println("NO");
    con.close();
  }
}   

Python implementation

Read the lengths of the triangle's sides.

a, b, c = map(int,input().split())

Check whether the triangle is right-angled and print the corresponding result.

if a * a + b * b == c * c or a * a + c * c == b * b 
                          or b * b + c * c == a * a:
  print("YES")
else:
  print("NO")