easy
2 views

Right Angle Triangle

Check if three given side lengths form a right-angled triangle using the Pythagorean theorem

Understand the Problem

Problem Statement

Given the lengths of three sides of a triangle (A, B, C), determine if the triangle is a right-angled triangle. A triangle is right-angled if the square of the longest side equals the sum of squares of the other two sides (Pythagorean theorem: a² + b² = c²).

Constraints

  • 1 ≤ A, B, C ≤ 9999
  • All inputs are positive integers representing side lengths
  • The triangle inequality must be satisfied for a valid triangle

Examples

Example 1
Input
5
3
4
Output
YES
Explanation

The sides are 3, 4, and 5. Sorting them gives 3, 4, 5. Checking: 3² + 4² = 9 + 16 = 25 = 5². Since 3² + 4² = 5², this forms a right-angled triangle.

Example 2
Input
11
6
9
Output
NO
Explanation

The sides are 6, 9, and 11. Sorting them gives 6, 9, 11. Checking: 6² + 9² = 36 + 81 = 117, but 11² = 121. Since 6² + 9² ≠ 11², this does not form a right-angled triangle.

Solution

#include <stdio.h>
#include <math.h>

int main() {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    
    // Sort the sides to identify the largest
    if (a > b) {
        int temp = a;
        a = b;
        b = temp;
    }
    if (b > c) {
        int temp = b;
        b = c;
        c = temp;
    }
    if (a > b) {
        int temp = a;
        a = b;
        b = temp;
    }
    
    // Check Pythagorean theorem: a² + b² = c²
    if (a*a + b*b == c*c) {
        printf("YES\n");
    } else {
        printf("NO\n");
    }
    
    return 0;
}
Time:O(1) - Constant time operations regardless of input size
Space:O(1) - Uses only a fixed number of integer variables
Approach:

The C solution follows these steps:

  1. Read three integers representing the side lengths
  2. Sort the three values using simple swap operations to identify the longest side (c)
  3. <3>Check if a² + b² equals c² using integer arithmetic
  4. Print "YES" if the Pythagorean theorem holds, otherwise "NO"

The sorting ensures we always compare the two shorter sides against the longest side.

Visual Explanation

Loading diagram...