Given an integer array nums
, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0
.
Example 1:
Input: nums = [2,1,2] Output: 5 Explanation: You can form a triangle with three side lengths: 1, 2, and 2.
Example 2:
Input: nums = [1,2,1,10] Output: 0 Explanation: 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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the largest perimeter triangle from a set of side lengths means we will check every possible combination of three sides. We'll then determine if those three sides can form a valid triangle, and if so, calculate its perimeter.
Here's how the algorithm would work step-by-step:
def largest_perimeter_triangle(side_lengths):
number_of_sides = len(side_lengths)
largest_perimeter = 0
for first_side_index in range(number_of_sides):
for second_side_index in range(first_side_index + 1, number_of_sides):
for third_side_index in range(second_side_index + 1, number_of_sides):
first_side = side_lengths[first_side_index]
second_side = side_lengths[second_side_index]
third_side = side_lengths[third_side_index]
# Triangle inequality theorem: sum of any two sides > third side
if first_side + second_side > third_side and \
first_side + third_side > second_side and \
second_side + third_side > first_side:
current_perimeter = first_side + second_side + third_side
# Keep track of largest valid perimeter
if current_perimeter > largest_perimeter:
largest_perimeter = current_perimeter
return largest_perimeter
To find the largest triangle perimeter, we want to quickly find the three longest sides that actually form a triangle. Instead of checking all combinations, we sort the sides and work backwards, making it efficient.
Here's how the algorithm would work step-by-step:
def largest_perimeter(nums): nums.sort(reverse=True)
for i in range(len(nums) - 2):
# Iterate, ensuring at least three sides are available
longest_side = nums[i]
second_longest_side = nums[i+1]
shortest_side = nums[i+2]
# The triangle inequality theorem must hold.
if second_longest_side + shortest_side > longest_side:
return longest_side + second_longest_side + shortest_side
# If no such triangle can be formed, return 0
return 0
Case | How to Handle |
---|---|
Empty input array | Return 0 immediately since no triangle can be formed. |
Input array with less than 3 elements | Return 0 immediately, as a triangle requires at least three sides. |
Input array contains negative numbers | Return 0, because side lengths must be positive. |
Input array contains zero values | Zero length sides are invalid for triangle formation, handle them normally. |
Input array with very large positive integer values that could cause overflow during perimeter calculation | Consider using a larger data type or checking for overflow before returning the perimeter. |
Input array with all identical values, forming an equilateral triangle | The solution should correctly calculate the perimeter of the equilateral triangle. |
Input array with values that cannot form a valid triangle (e.g., 1, 2, 5) | The solution should return 0 if no combination of three values satisfies the triangle inequality theorem. |
Input array already sorted in descending order | The solution needs to handle correctly already sorted input array. |