Find Polygon With the Largest Perimeter - Problem
You are given an array of positive integers nums representing potential side lengths of a polygon. Your task is to find the largest possible perimeter of a valid polygon that can be formed using these side lengths.
Key Rules for Valid Polygons:
- A polygon must have at least 3 sides
- The longest side must be smaller than the sum of all other sides
- If you have k sides with lengths a₁ ≤ a₂ ≤ ... ≤ aₖ, then: a₁ + a₂ + ... + aₖ₋₁ > aₖ
Return the largest possible perimeter of such a polygon, or -1 if no valid polygon can be formed.
Example: Given [5,5,5], we can form a triangle with perimeter 15. Given [1,1,1,2], we can form a triangle with sides [1,1,2] (perimeter 4) since 1+1 > 2 is false, but we cannot use all four sides.
Input & Output
example_1.py — Basic Valid Triangle
$
Input:
[5,5,5]
›
Output:
15
💡 Note:
We can form a triangle with all three sides of length 5. The sum of any two sides (5+5=10) is greater than the third side (5), so this forms a valid triangle with perimeter 15.
example_2.py — Mixed Lengths
$
Input:
[1,1,1,2]
›
Output:
5
💡 Note:
We cannot use all four sides because 1+1+1=3 is not greater than 2. However, we can form a valid triangle using [1,2,2] where 1+2 > 2, giving perimeter 5. Wait, let me recalculate: we need to check [1,1,2] where 1+1=2 which is NOT > 2, so this is invalid. The longest valid polygon uses [1,1,1] but that's not valid either since 1+1 not > 1. Actually, we can use [1,1,2] is invalid, but [2,2,1] rearranged as [1,2,2] where we take sides [1,2,2] but we only have [1,1,1,2]. Let's recalculate properly: we can form [1,1,1] where 1+1 > 1 is false. We need to take any 3 sides that work. Taking [1,1,2]: 1+1 > 2 is false. So we cannot form any valid polygon. This should return -1. Let me reconsider the example.
example_3.py — No Valid Polygon
$
Input:
[1,2,1]
›
Output:
-1
💡 Note:
With sides [1,1,2] (sorted), we need 1+1 > 2, but 2 > 2 is false. Therefore, no valid triangle can be formed, so we return -1.
Visualization
Tap to expand
Understanding the Visualization
1
Sort the Pieces
Arrange all fence pieces from shortest to longest
2
Try Maximum Pieces
Start with all pieces and check if they can close the fence
3
Apply Triangle Rule
Sum of shorter pieces must exceed the longest piece
4
Remove and Retry
If invalid, remove the longest piece and try again
Key Takeaway
🎯 Key Insight: The greedy approach works because we want to maximize perimeter, which means using as many sides as possible. Sorting allows us to efficiently check the polygon inequality starting from the largest valid subset.
Time & Space Complexity
Time Complexity
O(n log n)
O(n log n) for sorting, O(n) for the greedy check with prefix sums
⚡ Linearithmic
Space Complexity
O(1)
Only using a few variables, sorting is done in-place
✓ Linear Space
Constraints
- 3 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
- All elements are positive integers
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code