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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 1041 <= nums[i] <= 106Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Greedy
[2,1,2]
[1,2,1,10]
largest-triangle-area)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #976: Largest Perimeter Triangle
class Solution {
public int largestPerimeter(int[] nums) {
Arrays.sort(nums);
for (int i = nums.length - 1; i >= 2; --i) {
int c = nums[i - 1] + nums[i - 2];
if (c > nums[i]) {
return c + nums[i];
}
}
return 0;
}
}
// Accepted solution for LeetCode #976: Largest Perimeter Triangle
func largestPerimeter(nums []int) int {
sort.Ints(nums)
for i := len(nums) - 1; i >= 2; i-- {
if c := nums[i-1] + nums[i-2]; c > nums[i] {
return c + nums[i]
}
}
return 0
}
# Accepted solution for LeetCode #976: Largest Perimeter Triangle
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
nums.sort()
for i in range(len(nums) - 1, 1, -1):
if (c := nums[i - 1] + nums[i - 2]) > nums[i]:
return c + nums[i]
return 0
// Accepted solution for LeetCode #976: Largest Perimeter Triangle
impl Solution {
pub fn largest_perimeter(mut nums: Vec<i32>) -> i32 {
let n = nums.len();
nums.sort_unstable_by(|a, b| b.cmp(&a));
for i in 2..n {
let (a, b, c) = (nums[i - 2], nums[i - 1], nums[i]);
if a < b + c {
return a + b + c;
}
}
0
}
}
// Accepted solution for LeetCode #976: Largest Perimeter Triangle
function largestPerimeter(nums: number[]): number {
nums.sort((a, b) => a - b);
for (let i = nums.length - 1; i > 1; --i) {
const [a, b, c] = nums.slice(i - 2, i + 1);
if (a + b > c) {
return a + b + c;
}
}
return 0;
}
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
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.