Find Polygon With the Largest Perimeter - Complete Solution Guide
Find Polygon With the Largest Perimeter is LeetCode problem 2971, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 a 1 , a 2 , a 3 , ..., a k where a 1 <= a 2 <= a 3 <= ... <= a k and a 1 + a 2 + a 3 + ... + a k-1 > a k , then there always exists a polygon with k sides whose lengths are a 1 , a 2 , a 3 , ..., a k . The perimeter of a polygon is the sum of
Detailed Explanation
The problem asks us to find the largest possible perimeter of a polygon that can be formed using the given array of positive integers `nums` as side lengths. A polygon must have at least 3 sides. The key constraint is that the length of the longest side must be strictly less than the sum of the lengths of the other sides. If no such polygon can be formed, we return -1.
Solution Approach
The solution first sorts the input array `nums` in ascending order. Then, it calculates the total sum of all the numbers in the array. The solution iterates through the sorted array from right to left (largest to smallest number) considering each number as the longest side of a potential polygon. For each longest side, the sum of the remaining numbers is compared to the longest side. If the sum of the remaining numbers is greater than the longest side, a valid polygon can be formed using these numbers, and the total sum is returned as the largest possible perimeter. If no such polygon can be formed after checking all possible longest sides, -1 is returned.
Step-by-Step Algorithm
- Step 1: Sort the input array `nums` in ascending order.
- Step 2: Calculate the total sum of all elements in `nums`.
- Step 3: Iterate through the array `nums` from right to left (from the largest element to the third element) using a loop with index `i`.
- Step 4: In each iteration, consider `nums[i]` as the longest side of the polygon.
- Step 5: Calculate the sum of the other sides by subtracting `nums[i]` from the total sum.
- Step 6: Check if the sum of the other sides is greater than `nums[i]`. If it is, return the total sum as the largest possible perimeter.
- Step 7: If the sum of the other sides is not greater than `nums[i]`, subtract `nums[i]` from the total sum and continue the iteration.
- Step 8: If the loop completes without finding a valid polygon, return -1.
Key Insights
- Insight 1: Sorting the input array allows us to efficiently consider the longest side candidates in descending order, and easily calculate the sum of the smaller sides.
- Insight 2: Using a prefix sum approach (although implicitly) allows to quickly determine if the sum of the shorter sides exceeds the length of the current longest side candidate.
- Insight 3: Iterating from the largest number to the smallest allows us to stop as soon as we find a solution because the numbers are sorted. Smaller numbers cannot form a greater perimeter.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(1)
Topics
This problem involves: Array, Greedy, Sorting, Prefix Sum.
Companies
Asked at: Airtel.