Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[5,5,5]
[1,12,1,2,5,50,3]
[5,5,50]
3sum-smaller)valid-triangle-number)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2971: Find Polygon With the Largest Perimeter
class Solution {
public long largestPerimeter(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
long[] s = new long[n + 1];
for (int i = 1; i <= n; ++i) {
s[i] = s[i - 1] + nums[i - 1];
}
long ans = -1;
for (int k = 3; k <= n; ++k) {
if (s[k - 1] > nums[k - 1]) {
ans = Math.max(ans, s[k]);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2971: Find Polygon With the Largest Perimeter
func largestPerimeter(nums []int) int64 {
sort.Ints(nums)
n := len(nums)
s := make([]int, n+1)
for i, x := range nums {
s[i+1] = s[i] + x
}
ans := -1
for k := 3; k <= n; k++ {
if s[k-1] > nums[k-1] {
ans = max(ans, s[k])
}
}
return int64(ans)
}
# Accepted solution for LeetCode #2971: Find Polygon With the Largest Perimeter
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
nums.sort()
s = list(accumulate(nums, initial=0))
ans = -1
for k in range(3, len(nums) + 1):
if s[k - 1] > nums[k - 1]:
ans = max(ans, s[k])
return ans
// Accepted solution for LeetCode #2971: Find Polygon With the Largest Perimeter
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2971: Find Polygon With the Largest Perimeter
// class Solution {
// public long largestPerimeter(int[] nums) {
// Arrays.sort(nums);
// int n = nums.length;
// long[] s = new long[n + 1];
// for (int i = 1; i <= n; ++i) {
// s[i] = s[i - 1] + nums[i - 1];
// }
// long ans = -1;
// for (int k = 3; k <= n; ++k) {
// if (s[k - 1] > nums[k - 1]) {
// ans = Math.max(ans, s[k]);
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2971: Find Polygon With the Largest Perimeter
function largestPerimeter(nums: number[]): number {
nums.sort((a, b) => a - b);
const n = nums.length;
const s: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] + nums[i];
}
let ans = -1;
for (let k = 3; k <= n; ++k) {
if (s[k - 1] > nums[k - 1]) {
ans = Math.max(ans, s[k]);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.