Skip to main content
Back to problems
Leetcode
Medium
Arrays
Sorting
Math
Greedy
Largest Perimeter Triangle

Find the largest possible perimeter of a non-degenerate triangle that can be formed from three lengths in the array.

Acceptance 0%
Problem Statement

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 nums of positive integers.
  • Each value represents a possible side length.

Output Format

  • Return a single integer: the maximum perimeter of any valid triangle, or 0 if no valid triangle exists.

Constraints

  • 3 <= nums.length
  • 1 <= nums[i]
  • All values are integers.
  • Use a strict triangle inequality: a + b > c for sorted sides a <= b <= c.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.