Largest Perimeter Triangle

Given an integer array nums, return the largest perimeter of a triangle with a non-zero area that can be formed from three of these lengths. If it is impossible to form any triangle of non-zero area, return 0.

30:00

Largest Perimeter Triangle
easy
Free written solution
Topics
Companies

Given an integer array nums, return the largest perimeter of a triangle with a non-zero area that can be formed from three of these lengths. If it is impossible to form any triangle of non-zero area, return 0.

Example 1:
Input: {"nums":[2,1,2]}
Output: 5
Constraints:
  • 3≤nums.length≤1043 \leq \text{nums.length} \leq 10^4

  • 1≤nums[i]≤1061 \leq \text{nums}[i] \leq 10^6

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[2,1,2]}

We have 3 sticks. Three sticks make a real triangle only if the two shorter ones together are longer than the longest.

sticks as given

2

1

2

Variables
VariableValue
n3
DepthFunction Call
Stack empty
0/3