Find the largest possible perimeter of a non-degenerate triangle that can be formed from three lengths in the array.
Problem
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.
Goal
Determine the maximum perimeter among all valid triangles that can be built from the given lengths.
Input Format
- An integer array
numsof positive integers. - Each value represents a possible side length.
Output Format
- Return a single integer: the maximum perimeter of any valid triangle, or
0if no valid triangle exists.
Constraints
3 <= nums.length1 <= nums[i]- All values are integers.
- Use a strict triangle inequality:
a + b > cfor sorted sidesa <= 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.
Show 1 more example
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
Unlock deeper context for this problem
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.