Ctrl K

Triangle From Sticks

Easy
You have a set of sticks whose lengths are given in the array sticks. Pick exactly three of them to build a triangle with non-zero area, making its perimeter as large as possible. Three lengths form a real triangle only when the two shorter ones add up to strictly more than the longest. Return the largest perimeter you can get. If no three sticks work, return 0.
Example 1
sticks=[4, 9, 6, 3] → 19

9, 6 and 4 make a triangle (6 + 4 > 9) with perimeter 19, the largest possible.

Example 2
sticks=[2, 3, 5, 12] → 0

No triple works: for instance 2 + 3 = 5, so that "triangle" collapses into a segment. The answer is 0.

Example 3
sticks=[7, 7, 7] → 21
Constraints
  • 3 ≤ len(sticks) ≤ 10⁴
  • 1 ≤ sticks[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsUp or Down for Every Post MediumRound Prices to a Total MediumBiggest Number Divisible by Three HardThe Middle Bag MediumPrices That Fade With Each Sale Medium
loading Python…
Input
sticks=[4, 9, 6, 3]
Expected
19

Run checks the first 3 tests in your browser. Submit checks all 115 tests on the server, hidden ones included.