Ctrl K

Biggest Polygon From Sticks

Medium
You have a pile of sticks whose lengths are listed in sticks. Pick some of them to form a polygon: it needs at least 3 sides, and its longest side must be strictly shorter than the sum of all the other sides. Whenever that holds, a polygon with those sides always exists. Each stick can be used at most once. Return the largest possible perimeter (the sum of the side lengths) of such a polygon, or -1 if no polygon can be formed.
Example 1
sticks=[2, 3, 4, 20] → 9

The 20 stick is longer than all the others combined (9), so it cannot be used. 2, 3 and 4 make a triangle since 4 < 2 + 3, giving a perimeter of 9.

Example 2
sticks=[1, 2, 3] → -1

3 is not less than 1 + 2, so the shape collapses into a line and no polygon can be built.

Example 3
sticks=[6, 1, 1, 2, 3, 4] → 17
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 problemsStudios for Recording Sessions MediumArrange for the Busiest Ranges MediumEven Out With Increments MediumLevel the Jars MediumRug Over Painted Stretches Medium
loading Python…
Input
sticks=[2, 3, 4, 20]
Expected
9

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