← All questions
RareProgramming

Write a program that prints YES if three positive integers can be the sides of a nondegenerate triangle, and NO otherwise.

Short answer
a ≤ b ≤ c, a + b > c → YES, a + b ≤ c → NO

Explanation

Sort the lengths into increasing order and name them a, b and c. Then c is the longest. Since the lengths are positive, the two sums that already contain c exceed the remaining side. The remaining test is a + b > c.

The split() call separates the input at whitespace, so a space or a line break works. Converting each entry with int makes comparisons depend on its numerical value.

import sys

a, b, c = sorted(map(int, sys.stdin.read().split()))
print("YES" if a + b > c else "NO")

The inequality is strict. If the shorter lengths add up to the longest, they lie along a single segment and enclose zero area. Permuting the inputs leaves the decision unchanged.

Input Check Output
5 3 4 3 + 4 > 5 YES
3 1 2 1 + 2 = 3 NO
1 5 2 1 + 2 < 5 NO
Conditionals

Platforms

  • MAXIMUM

Download now and try it for free on your own tasks!

10 15 free credits • No credit card required
Version: Loading…

Choose the Windows app or the Chrome extension with phone controls.