Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Largest Perimeter Triangle

XPChallenge Points: 10
levelLevel: Easy

You are given an integer array nums, where each value represents the length of a side.

Your task is to return the largest possible perimeter of a triangle that has non-zero area and can be formed using any three lengths from the array.

A triangle is valid if it satisfies the triangle inequality:

a + b > c

(where c is the largest side)

If no valid triangle can be formed, return 0.

Example 1:

Input: nums = [2, 1, 2]

Output: 5

Explanation:

A triangle can be formed with sides 1, 2, and 2, and the perimeter is 5.

Example 2:

Input: nums = [1, 2, 1, 10]

Output: 0

Explanation:

No three lengths satisfy the triangle inequality. So no triangle with non-zero area can be formed.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result