LeetCode Problem

How to Solve Largest Perimeter Triangle

To solve Largest Perimeter Triangle, sort the array and check from largest to smallest if a valid triangle can be formed. This leverages greedy choice and invariant validation for optimal performance. If no triangle can be formed, return 0.

GhostInterview Help

Need help with Largest Perimeter Triangle without spending extra time grinding it?

GhostInterview can read Largest Perimeter Triangle from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #976Greedy choice plus invariant validationReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Greedy choice plus invariant validation
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve Largest Perimeter Triangle, sort the array and check from largest to smallest if a valid triangle can be formed. This leverages greedy choice and invariant validation for optimal performance. If no triangle can be formed, return 0.

Problem Statement

Given an integer array nums, return the largest perimeter of a triangle formed from any three side lengths. A triangle is valid if the sum of the lengths of any two sides is greater than the third side. If no valid triangle can be formed, return 0.

For example, with nums = [2, 1, 2], you can form a triangle with sides 1, 2, and 2, resulting in a perimeter of 5. In contrast, with nums = [1, 2, 1, 10], no valid triangle can be formed, so the output is 0.

Examples

Example 1

Input: nums = [2,1,2]

Output: 5

You can form a triangle with three side lengths: 1, 2, and 2.

Example 2

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

Output: 0

You cannot use the side lengths 1, 1, and 2 to form a triangle. You cannot use the side lengths 1, 1, and 10 to form a triangle. You cannot use the side lengths 1, 2, and 10 to form a triangle. As we cannot use any three side lengths to form a triangle of non-zero area, we return 0.

Constraints

  • 3 <= nums.length <= 104
  • 1 <= nums[i] <= 106

Solution Approach

Sort the array

First, sort the array in descending order. This allows for checking the largest possible triangles first and ensures that the greedy choice of the largest values is always considered.

Check for a valid triangle

After sorting, check for any consecutive triplet of numbers in the sorted array that satisfies the triangle inequality: the sum of the first two sides must be greater than the third side. If this condition is met, return the perimeter of these three sides.

Return 0 if no triangle is valid

If no triplet satisfies the triangle inequality, return 0. This means it is impossible to form a triangle with the given side lengths.

Complexity Analysis

MetricValue
TimeO(N \log N)
SpaceO(1)

Sorting the array takes O(N log N) time, where N is the length of the array. Checking for a valid triangle after sorting takes O(1) for each triplet. Hence, the overall time complexity is O(N log N), and space complexity is O(1) because no additional space is required outside of the input array.

What Interviewers Usually Probe

  • Ability to recognize the greedy approach in problems involving optimization like this one.
  • Understanding of the triangle inequality and how it applies to the formation of valid triangles.
  • Efficient handling of sorting and triangle validation within the time complexity constraint.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to check the triangle inequality after sorting the array.
  • Not considering the largest possible triangle first, leading to suboptimal solutions.
  • Overcomplicating the problem by not leveraging sorting to reduce unnecessary checks.

Follow-up variants

  • What if we allow more than one set of triangle sides to form? Could we maximize the sum of perimeters?
  • What if the array contains duplicate values? Does the approach still hold?
  • What if instead of maximizing the perimeter, we need to minimize it? How does this change the solution?

How GhostInterview Helps

  • GhostInterview offers step-by-step guidance to ensure you follow the greedy approach correctly while maintaining the triangle inequality condition.
  • With structured practice, GhostInterview enables faster recognition of sorting-based problems and optimal greedy solutions.
  • It helps reinforce the understanding of the triangle inequality, making it easier to spot and solve similar problems efficiently.

Topic Pages

FAQ

What is the pattern for solving the Largest Perimeter Triangle problem?

The problem is best approached using greedy choice and invariant validation. After sorting the array, check consecutive triplets for the triangle inequality to find the largest valid perimeter.

Why do we sort the array in descending order?

Sorting in descending order allows you to check the largest possible triangles first, optimizing for the largest perimeter while ensuring a valid triangle.

What should we do if no valid triangle is found?

If no valid triangle can be formed from any triplet, return 0 as the perimeter.

How do we check if three sides can form a valid triangle?

For three sides a, b, and c, they form a valid triangle if a + b > c, b + c > a, and a + c > b.

What is the time complexity of this approach?

The time complexity is O(N log N) due to the sorting step, and the space complexity is O(1) because no additional data structures are used.

GhostInterview Solver

Need direct help with Largest Perimeter Triangle instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Largest Perimeter Triangle from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.