Choose Three Lengths with the Largest Nondegenerate Triangle Perimeter
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
### Problem
Choose three occurrences from `nums` as side lengths. Return the largest possible perimeter of a triangle with positive area, or0 if no such triangle exists.
```cpp
class Solution {
public:
int largestPerimeter(vector<int>& nums);
};
```
### Examples
**Example 1:**
```text
Input: nums = [4,5,9,10]
Output: 24
Explanation: Sides5,9,10 satisfy5+9>10 and give the maximum perimeter24.
```
**Example 2:**
```text
Input: nums = [1,2,3]
Output: 0
Explanation: The only triple has1+2=3, so its area would be zero.
```
### Constraints
- `3 <= nums.length <= 10^4`.
- `1 <= nums[i] <= 10^6`.
### Contract considerations
Positive area excludes a degenerate straight-line triple. The output is the perimeter, not the side array.
```hint Read the boundary condition
For a fixed largest side, only the two largest available smaller positions can maximize its chance of forming a triangle.
```
### Boundary reminders
Positive area excludes a degenerate straight-line triple. The output is the perimeter, not the side array.
Overview: Choose Three Lengths with the Largest Nondegenerate Triangle Perimeter. Choose three occurrences from nums as side lengths.
Choose Three Lengths with the Largest Nondegenerate Triangle Perimeter
Google
Jun 22, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0
Problem
Choose three occurrences from nums as side lengths. Return the largest possible perimeter of a triangle with positive area, or0 if no such triangle exists.
class Solution {
public:
int largestPerimeter(vector<int>& nums);
};
Examples
Example 1:
Input: nums = [4,5,9,10]
Output: 24
Explanation: Sides5,9,10 satisfy5+9>10 and give the maximum perimeter24.
Example 2:
Input: nums = [1,2,3]
Output: 0
Explanation: The only triple has1+2=3, so its area would be zero.
Constraints
3 <= nums.length <= 10^4
.
1 <= nums[i] <= 10^6
.
Contract considerations
Positive area excludes a degenerate straight-line triple. The output is the perimeter, not the side array.
Boundary reminders
Positive area excludes a degenerate straight-line triple. The output is the perimeter, not the side array.