Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

610. Triangle Judgement - Leetcode Solution

Problem Description

The Triangle Judgement problem asks you to determine whether three given integers can represent the lengths of the sides of a triangle. Specifically, you are given three positive integers, a, b, and c. Your task is to check if these three values can form a valid triangle.

The key mathematical constraint is the triangle inequality theorem, which states: For any three lengths to form a triangle, the sum of the lengths of any two sides must be greater than the length of the remaining side. That is, all of the following must be true:

  • a + b > c
  • a + c > b
  • b + c > a

If these conditions are satisfied, output Yes (they can form a triangle). Otherwise, output No.

Thought Process

The first step is to understand what makes three lengths form a triangle. The triangle inequality theorem is the key: the sum of any two sides must be greater than the third side. A brute-force approach would be to check all combinations, but since there are only three sides, it's straightforward to check all three pairs.

We should also consider edge cases, such as when the sum of two sides is equal to the third (which does not form a triangle), or if any side is zero or negative (not allowed, given the "positive integers" constraint). Since the problem only asks for a simple check, we can directly apply the triangle inequalities.

The problem is direct and does not require optimization or complex data structures. The main challenge is correctly applying the triangle inequality and returning the correct result.

Solution Approach

Let’s break down the solution into clear steps:

  1. Input Reading: Read three positive integers, a, b, and c. Depending on the language, these may be provided as function arguments, from standard input, or as an array.
  2. Apply Triangle Inequality:
    • Check if a + b > c
    • Check if a + c > b
    • Check if b + c > a
    All three conditions must be true for the lengths to form a triangle.
  3. Return Result:
    • If all conditions are true, return or output "Yes"
    • If any condition fails, return or output "No"

This approach is efficient and direct because only three comparisons are needed.

Example Walkthrough

Let’s walk through a sample input:

  • a = 2
  • b = 3
  • c = 4

Check the triangle inequalities:

  • 2 + 3 = 5 > 4 (True)
  • 2 + 4 = 6 > 3 (True)
  • 3 + 4 = 7 > 2 (True)
Since all conditions are true, the output is Yes.

Another example:

  • a = 1
  • b = 2
  • c = 3
  • 1 + 2 = 3 > 3 (False, since 3 is not greater than 3)
  • 1 + 3 = 4 > 2 (True)
  • 2 + 3 = 5 > 1 (True)
Since not all conditions are true, the output is No.

Code Implementation

def triangle_judgement(a, b, c):
    if (a + b > c) and (a + c > b) and (b + c > a):
        return "Yes"
    else:
        return "No"

# Example usage:
# print(triangle_judgement(2, 3, 4))  # Output: Yes
# print(triangle_judgement(1, 2, 3))  # Output: No
      
#include <iostream>
using namespace std;

string triangleJudgement(int a, int b, int c) {
    if ((a + b > c) && (a + c > b) && (b + c > a)) {
        return "Yes";
    } else {
        return "No";
    }
}

// Example usage:
// cout << triangleJudgement(2, 3, 4) << endl; // Output: Yes
// cout << triangleJudgement(1, 2, 3) << endl; // Output: No
      
public class TriangleJudgement {
    public static String triangleJudgement(int a, int b, int c) {
        if ((a + b > c) && (a + c > b) && (b + c > a)) {
            return "Yes";
        } else {
            return "No";
        }
    }

    // Example usage:
    // System.out.println(triangleJudgement(2, 3, 4)); // Output: Yes
    // System.out.println(triangleJudgement(1, 2, 3)); // Output: No
}
      
function triangleJudgement(a, b, c) {
    if ((a + b > c) && (a + c > b) && (b + c > a)) {
        return "Yes";
    } else {
        return "No";
    }
}

// Example usage:
// console.log(triangleJudgement(2, 3, 4)); // Output: Yes
// console.log(triangleJudgement(1, 2, 3)); // Output: No
      

Time and Space Complexity

Brute-force Approach: In this problem, the brute-force and optimal approaches are essentially the same, since there are only three comparisons to make.

  • Time Complexity: O(1) (constant time), because we perform a fixed number of arithmetic and comparison operations, regardless of input size.
  • Space Complexity: O(1) (constant space), since no additional data structures are used.

There is no way to further optimize this problem, as the direct check is already optimal.

Summary

The Triangle Judgement problem is a straightforward application of the triangle inequality theorem. By directly checking whether the sum of any two sides exceeds the third side, we can determine if three lengths can form a triangle. The solution is efficient, elegant, and easy to implement in any major programming language. Understanding the underlying mathematical rule is the key insight for this problem.