Find the largest possible perimeter of a non-degenerate triangle that can be formed from three lengths in the array.
You are given an array of positive integers representing side lengths.
Choose any three lengths that can form a non-degenerate triangle. Among all valid choices, return the largest possible perimeter.
A triangle with sides a, b, and c is valid only if the sum of the two smaller sides is strictly greater than the largest side.
If no three lengths can form a triangle, return 0.
Determine the maximum perimeter among all valid triangles that can be built from the given lengths.
nums of positive integers.0 if no valid triangle exists.3 <= nums.length1 <= nums[i]a + b > c for sorted sides a <= b <= c.Example 1
Input
nums = [2,1,2]
Output
5
Explanation
The lengths 1, 2, and 2 form a valid triangle with perimeter 5.
Example 2
Input
nums = [1,2,1,10]
Output
0
Explanation
No three lengths satisfy the strict triangle inequality.
Example 3
Input
nums = [3,6,2,3]
Output
8
Explanation
The valid triangle [2,3,3] has perimeter 8, which is the largest possible.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.