QUESTION #020

Check if Three Sides Form a Valid Triangle

Easy

📋 Problem Statement

Write a program to check whether the given three sides can form a triangle or not.

📐 Triangle Inequality Theorem

For three sides a, b, and c to form a valid triangle, they must satisfy:

  • The sum of any two sides must be greater than the third side
  • a + b > c
  • a + c > b
  • b + c > a

All three conditions must be true simultaneously.


/│\
/ │ \
/ │ \ c
a / │ \
/ │h \
/ │ \
/______|______\
b

A valid triangle: a + b > c, a + c > b, b + c > a

📥 Input Format

Accept three integer as a input

📤 Output Format

Print the output as "Valid" or "Not Valid"

💡 Examples

Example 1:
Input: 8 9 2
Output: Valid
Verification:
• a = 8, b = 9, c = 2
• Check 1: a + b > c → 8 + 9 = 17 > 2 ✓
• Check 2: a + c > b → 8 + 2 = 10 > 9 ✓
• Check 3: b + c > a → 9 + 2 = 11 > 8 ✓
Result: All conditions satisfied → Valid triangle
Example 2:
Input: 9 5 4
Output: Not Valid
Verification:
• a = 9, b = 5, c = 4
• Check 1: a + b > c → 9 + 5 = 14 > 4 ✓
• Check 2: a + c > b → 9 + 4 = 13 > 5 ✓
• Check 3: b + c > a → 5 + 4 = 9 NOT > 9 ✗ (equal, not greater)
Result: One condition failed → Not a valid triangle
Example 3:
Input: 1 2 10
Output: Not Valid
Verification:
• a = 1, b = 2, c = 10
• Check 1: a + b > c → 1 + 2 = 3 NOT > 10 ✗
Result: First condition failed → Not a valid triangle
(The sides 1 and 2 can't reach across to connect with a side of length 10)

⚠️ Constraints

1 <= INPUT <= 10^15

✅ Solution

#include <iostream>
using namespace std;

int main() {
    long long a, b, c;
    cin >> a >> b >> c;

    // Check for triangle validity using the triangle inequality theorem
    if ((a + b > c) && (a + c > b) && (b + c > a))
        cout << "Valid";
    else
        cout << "Not Valid";

    return 0;
}

🔑 Key Concepts

  • Triangle Inequality Theorem: Fundamental geometric principle for triangle validation
  • Compound Conditions: Using multiple conditions with AND operator (&&)
  • All Conditions Required: All three inequalities must be true for a valid triangle
  • Long Long Data Type: Handling large values up to 10^15
  • Geometric Validation: Applying mathematical theorems in programming
  • Greater Than (not equal): Using > instead of >= is crucial

💡 Why This Theorem Works

Imagine trying to build a triangle with sticks:

  • Valid Triangle: If you have sides 3, 4, 5 - you can connect all three ends
  • Invalid Triangle: If you have sides 1, 2, 10 - the short sides can't span the gap
  • Edge Case: If sides are 3, 4, 7 - they form a straight line (3 + 4 = 7), not a triangle

Key Insight: The sum of two smaller sides must be strictly greater than the largest side to "close" the triangle.

⚠️ Common Mistakes to Avoid

  • Using >= instead of >: Equal values form a straight line, not a triangle (e.g., 3 + 4 = 7)
  • Checking only one condition: All three conditions must be verified
  • Using OR (||) instead of AND (&&): All conditions must be true simultaneously
  • Wrong operator precedence: Missing parentheses can change logic evaluation
  • Data type overflow: Using int instead of long long for large values
  • Checking only largest side: You can't assume which side is largest without checking

🚀 Alternative Approach: Check Maximum Side

You can optimize by finding the maximum side first:

// Find the maximum side
long long max_side = max({a, b, c});
long long sum_other_two = a + b + c - max_side;

// Only need to check if sum of two smaller > largest
if (sum_other_two > max_side)
    cout << "Valid";
else
    cout << "Not Valid";

Why this works: If the two smaller sides sum to more than the largest, all three triangle inequality conditions are automatically satisfied.

🎯 What You'll Learn

  • Applying mathematical theorems in programming (triangle inequality)
  • Working with compound logical conditions using AND operator
  • Understanding the importance of strict inequality (> vs >=)
  • Geometric validation and computational geometry basics
  • Writing clean, readable conditional statements
  • Edge case handling (degenerate triangles)

🌍 Real-world Applications

  • Computer Graphics: Validating triangular meshes in 3D modeling
  • Game Development: Collision detection and hitbox validation
  • CAD Software: Engineering design validation
  • GPS/Navigation: Triangulation for location finding
  • Architecture: Structural stability calculations
  • Manufacturing: Quality control for triangular parts
  • Surveying: Land measurement and mapping

🎓 Extension: Types of Triangles

Once you know it's a valid triangle, you can classify it:

  • Equilateral: All three sides equal (a == b == c)
  • Isosceles: Two sides equal (a == b || b == c || a == c)
  • Scalene: All sides different
  • Right Triangle: a² + b² = c² (Pythagorean theorem)
  • Obtuse: One angle > 90° (a² + b² < c² for some arrangement)
  • Acute: All angles < 90° (a² + b² > c² for all arrangements)