Skip to main content

Valid Triangle from Three Sides

EasyProblem

Description

Given three side lengths a, b and c, return true if they can form a triangle and false if they can't.

Three sides make a triangle when each one is shorter than the other two together. If two sides add up to exactly the third, the three lie flat in a line, which doesn't count.

Examples

Example 1

Input: a = 3, b = 4, c = 5

Output: true

Explanation: Each side is shorter than the other two together: 3 + 4 > 5, 3 + 5 > 4 and 4 + 5 > 3.

Example 2

Input: a = 1, b = 2, c = 3

Output: false

Explanation: 1 + 2 is exactly 3, so the three sides lie flat in a line: not a triangle.

Example 3

Input: a = 2, b = 10, c = 3

Output: false

Explanation: 2 + 3 is 5, shorter than 10, so the two short sides can't reach across the long one.

Example 4

Input: a = 7, b = 7, c = 7

Output: true

Explanation: Three equal sides always make a triangle.

Constraints

  • 1 ≤ a, b, c ≤ 2^31 - 1

Editorial

Approach - Check Every Pair

Intuition

Lay the longest side down. The other two have to meet above it, which they can only do if together they're longer than it. You don't know in advance which side is the longest, so check all three: a + b > c, a + c > b and b + c > a. Sorting the three sides first works too; then only the two short ones against the long one can fail.

In Java and C++, two sides of up to 2^31 - 1 add up past the largest int, so add them as a long (long long in C++). Python's integers don't overflow, and JavaScript's numbers hold these sums exactly.

Step-by-Step Explanation

Let's trace Example 3, a = 2, b = 10, c = 3:

  1. a + b = 2 + 10 = 12, more than c = 3.
  2. a + c = 2 + 3 = 5, not more than b = 10: these two can't reach across it.
  3. One pair fails, so return false.

Every Pair — The three sides; each step adds two of them and tests the sum against the third.

Algorithm

  1. If a + b > c, a + c > b and b + c > a, return true.
  2. Otherwise return false.

Code

class Solution {
public:
    bool isValidTriangle(int a, int b, int c) {
        // each side must be shorter than the other two together; long long, since a + b can pass the int range
        return (long long) a + b > c && (long long) a + c > b && (long long) b + c > a;
    }
};
class Solution:
    def isValidTriangle(self, a: int, b: int, c: int) -> bool:
        # each side must be shorter than the other two together
        return a + b > c and a + c > b and b + c > a
class Solution {
    public boolean isValidTriangle(int a, int b, int c) {
        // each side must be shorter than the other two together; long, since a + b can pass the int range
        return (long) a + b > c && (long) a + c > b && (long) b + c > a;
    }
}
var isValidTriangle = function(a, b, c) {
    // each side must be shorter than the other two together
    return a + b > c && a + c > b && b + c > a;
};

Complexity Analysis

Time Complexity: O(1)

Three sums and three comparisons.

Space Complexity: O(1)

Nothing is stored.

Common Mistakes

Each of these looks right on a quick read, and the tests still reject it. The outputs below come from running the code.

Allowing equal sides to count. a + b >= c

On a = 1, b = 2, c = 3 it returns true; the answer is false. When two sides add up to exactly the third, the three lie flat in a line, with no area: that isn't a triangle. The test has to be strictly greater, >, not >=.

Checking one pair. return a + b > c

On a = 10, b = 2, c = 3 it returns true; the answer is false. a + b > c only says c isn't too long. Any of the three sides can be the long one, so all three pairs need checking, or sort the sides and check the two short ones against the long one.

Adding big sides as ints. a + b > c && a + c > b

On a = 2147483647, b = 2147483647, c = 1 it returns false in Java; the answer is true. Two sides of up to 2^31 - 1 add up to as much as 4,294,967,294, past the largest int, 2,147,483,647. The sum wraps around to a negative number and the check fails; add them as a long.

Where This Pattern Is Taught

This problem practises checking every pair of sides, and adding two big ints without overflow. It's taught in Java for DSA and C++ for DSA.