You are given an array of positive integers nums of length n.
A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides.
Conversely, if you have k (k >= 3) positive real numbers a1, a2, a3, ..., ak where a1 <= a2 <= a3 <= ... <= ak and a1 + a2 + a3 + ... + ak-1 > ak, then there always exists a polygon with k sides whose lengths are a1, a2, a3, ..., ak.
The perimeter of a polygon is the sum of lengths of its sides.
Return the largest possible perimeter of a polygon whose sides can be formed from nums, or -1 if it is not possible to create a polygon.
Example 1:
Input: nums = [5,5,5] Output: 15 Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.
Example 2:
Input: nums = [1,12,1,2,5,50,3] Output: 12 Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12.
Example 3:
Input: nums = [5,5,50] Output: -1 Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.
Constraints:
3 <= n <= 1051 <= nums[i] <= 109When 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 goal is to find the largest polygon you can make from a set of line segments. A brute force approach means trying every possible combination of segments to see which one makes the biggest polygon.
Here's how the algorithm would work step-by-step:
from itertools import combinations
def find_largest_polygon_perimeter_brute_force(side_lengths):
number_of_sides = len(side_lengths)
largest_perimeter = 0
for polygon_size in range(3, number_of_sides + 1):
# Iterate over all possible combinations of sides
for combination in combinations(side_lengths, polygon_size):
if can_form_polygon(combination):
perimeter = sum(combination)
# Update the largest perimeter if necessary
largest_perimeter = max(largest_perimeter, perimeter)
return largest_perimeter
def can_form_polygon(sides):
# Check that the sum of all sides but the longest is greater than the longest side
longest_side = max(sides)
sum_of_other_sides = sum(sides) - longest_side
if sum_of_other_sides > longest_side:
return True
else:
return FalseTo find the polygon with the largest perimeter, we need to efficiently pick the longest possible sides. Sorting the sides allows us to quickly consider the longest ones first and check if they can form a valid polygon.
Here's how the algorithm would work step-by-step:
def find_polygon_with_largest_perimeter(sides):
sides.sort(reverse=True)
number_of_sides = len(sides)
for i in range(number_of_sides - 2):
# Iterate through the sides, checking polygon validity
longest_side = sides[i]
second_longest_side = sides[i+1]
third_longest_side = sides[i+2]
# Check if the three sides can form a valid polygon
if second_longest_side + third_longest_side > longest_side:
polygon_perimeter = longest_side + second_longest_side + third_longest_side
return polygon_perimeter
return 0| Case | How to Handle |
|---|---|
| Empty input array | Return -1 immediately as no polygon can be formed with no sides. |
| Input array with fewer than 3 elements | Return -1 because a polygon needs at least 3 sides. |
| Input array with very large numbers leading to potential integer overflow during perimeter calculation | Use a data type that can accommodate large numbers (e.g., long in Java or C++) or check for overflow before returning the result. |
| Input array containing all identical values | The algorithm should still correctly calculate the perimeter, ensuring at least three identical values are present. |
| Input array where no subset can form a valid polygon (e.g., [1, 2, 100]) | Return -1 as no combination of sides satisfies the polygon inequality theorem. |
| Input array with a very large number of elements to assess time complexity | Ensure the chosen algorithm has a time complexity of O(n log n) due to sorting (or better) to avoid exceeding time limits, if input sizes reach millions. |
| Input array contains only small values | Algorithm should accurately calculate the largest perimeter, even with smaller integers. |
| Array sorted in descending order | Sorting algorithm should correctly handle a reverse-sorted array without causing performance issues. |