Back to Beginner: I/O, Arithmetic, Loops
Q24Question 24 of 30

Form Triangle: print Yes if three lengths can form a valid triangle

Key Concepts
  • Three lengths form a triangle if and only if the sum of every pair is strictly greater than the third side.
  • Three conditions joined by AND: A+B > C, B+C > A, C+A > B; if any fails the triangle is invalid.
  • Strict greater-than is required because equality produces a degenerate (zero-area) triangle.
  • For money-like BigDecimal values, use add and compareTo for exact comparison at the boundary.
  • Optimization: if sorted so C is the largest, only A+B > C needs to be checked, since the other two are automatic.
  • Validate all three lengths are positive before checking the inequality.

Definition

Three lengths form a triangle only when every pair sums to strictly more than the third, checked as three AND-joined inequalities.
  • Triangle inequality – A+B > C, B+C > A, C+A > B; all three must hold.
  • Strict greater-than – equality (A+B == C) gives a degenerate triangle with zero area.
  • Single check if sorted – if C is the max, only A+B > C is needed; the other two are automatic.
  • BigDecimal – use a.add(b).compareTo(c) > 0 for exact boundary comparison.
  • Positive validation – zero or negative lengths are not valid sides.

National Bank of India validates that three separate limits are mutually consistent: no single limit can exceed the other two combined, mirroring the triangle inequality. For 3, 4, 5 the limits are balanced and pass. For 1, 2, 10 they are lopsided and fail.

Understanding the Concepts

What is the triangle inequality?

Three positive lengths can form a triangle if and only if the sum of every pair exceeds the third side. The three conditions are A+B > C, B+C > A, and C+A > B, all joined by AND.

How do you implement it?

JAVA
package com.bank.service;

import java.math.BigDecimal;

public class TriangleValidator {

    public static boolean canFormTriangle(BigDecimal a, BigDecimal b, BigDecimal c) {
        if (a.signum() <= 0 || b.signum() <= 0 || c.signum() <= 0) {
            return false; // sides must be positive
        }
        return a.add(b).compareTo(c) > 0
            && b.add(c).compareTo(a) > 0
            && c.add(a).compareTo(b) > 0;
    }

    public static void main(String[] args) {
        System.out.println(canFormTriangle(
            new BigDecimal("3"), new BigDecimal("4"), new BigDecimal("5")));
        System.out.println(canFormTriangle(
            new BigDecimal("1"), new BigDecimal("2"), new BigDecimal("10")));
    }
}
TEXT
true
false

Why strict greater-than?

If A+B equals C exactly, the three points are collinear and the triangle is flat with zero area. That is not a valid triangle, so the comparison must be strictly greater-than.

JAVA
// Degenerate: 1 + 2 == 3, not a triangle
System.out.println(canFormTriangle(
    new BigDecimal("1"), new BigDecimal("2"), new BigDecimal("3"))); // false

What is the sorted optimization?

  • If you sort the three sides so A <= B <= C, then:
  • A+B > C is the only check needed.
  • B+C > A is automatic because B >= A and C > 0.
  • C+A > B is automatic because C >= B.

This reduces three comparisons to one, but for only three values the savings is negligible.

How do you classify the triangle type?

Once the triangle is valid, you can classify it as equilateral (all sides equal), isosceles (exactly two sides equal), or scalene (all sides different). Use compareTo for each pair.

JAVA
public static String classifyTriangle(BigDecimal a, BigDecimal b, BigDecimal c) {
    if (!canFormTriangle(a, b, c)) return "Not a triangle";
    boolean ab = a.compareTo(b) == 0;
    boolean bc = b.compareTo(c) == 0;
    boolean ca = c.compareTo(a) == 0;
    if (ab && bc) return "Equilateral";
    if (ab || bc || ca) return "Isosceles";
    return "Scalene";
}
JAVA
System.out.println(classifyTriangle(
    new BigDecimal("5"), new BigDecimal("5"), new BigDecimal("5"))); // Equilateral
System.out.println(classifyTriangle(
    new BigDecimal("3"), new BigDecimal("4"), new BigDecimal("5"))); // Scalene

What are edge cases?

  • Zero length: not a valid side, return false.
  • Negative length: not meaningful, return false.
  • All equal (equilateral): always forms a triangle.
  • Two very large and one very small: passes if sum exceeds the large one.

Why use BigDecimal instead of double for boundary values?

Floating-point arithmetic can round at the boundary. For sides 1.0, 2.0, and 3.0000000000000004 (a double artifact), a floating-point comparison might incorrectly accept a degenerate triangle. BigDecimal performs exact arithmetic and gives the correct result.

What are common mistakes?

JAVA
// Wrong: using >= instead of >
return a.add(b).compareTo(c) >= 0; // accepts degenerate triangles!

// Wrong: checking only one inequality
return a.add(b).compareTo(c) > 0; // misses cases where A or B is too large

// Correct: all three with strict >
return a.add(b).compareTo(c) > 0
    && b.add(c).compareTo(a) > 0
    && c.add(a).compareTo(b) > 0;

What should a senior engineer know?

The triangle inequality is a general balance condition: no single element should dominate the other two combined. In banking, it validates that configuration limits are mutually consistent. Using BigDecimal with compareTo ensures the boundary comparison is exact, where floating-point might round and give a wrong result at the edge. At National Bank of India, Pankaj applied this same three-way balance check to validate that no single branch spending limit exceeded the other two combined.

What are common interview follow-up questions?

  • How do you classify the triangle as equilateral, isosceles, or scalene?
  • What is the area formula for a triangle given three sides? (Heron's formula)
  • Can you extend the inequality to four or more sides?

Visual Overview

DIAGRAM
  ┌──────────────┐
  │ Read A, B, C │
  └──────┬───────┘
         ▼
  ┌──────────────────────────────────────────────┐
  │ A+B > C  AND  B+C > A  AND  C+A > B ?       │
  └──┬──────────────────────────────────────┬────┘
     │ Yes                                  │ No
     ▼                                      ▼
  Print Yes                             Print No

Key Takeaway

Three lengths form a triangle if and only if the sum of every pair of sides is strictly greater than the remaining side. The condition is A+B > C, B+C > A, and C+A > B, all three joined by AND. If any single inequality fails, the three lengths cannot close into a triangle.

The comparisons must be strictly greater-than. If a pair sums to exactly the third side, the three points are collinear and the triangle is degenerate with zero area. All three sides must also be positive; a zero or negative side is meaningless.

For decimal values, use BigDecimal with add and compareTo so the comparison is exact right at the boundary, where floating-point would be risky. If sorted so the largest is last, only one inequality needs checking, but for three values all three checks are clear and cheap.

In a banking project at National Bank of India, this exact logic validated that three configuration limits were mutually balanced. A set like 3, 4, 5 passed, while 1, 2, 10 failed.

Remember one sentence: all three pair-sums must strictly exceed the third side, and equality makes it degenerate.

---

Share
Use ← → arrow keys to navigate