Interview Questions/Coding/Largest Festival Banner Frame

Largest Festival Banner Frame

Preview mode. Log in to edit, run, submit, and save progress.

Easy

Largest Festival Banner Frame

A festival crew has rods of different lengths and wants to build one triangular banner frame. A valid triangle can be built from three rods only if the sum of the two shorter rods is greater than the longest rod. You may choose any three rods from the available list. The goal is not merely to find any triangle, but to maximize the perimeter of the triangle frame. If every possible choice fails the triangle condition, no frame can be built. Input Format: You are given an integer array rods. Output Format: Return the largest possible triangle perimeter, or 0 if no valid triangle can be formed.

Examples

Example 1
Input:
rods = [2, 1, 2]
Output:
5

Explanation: Sorted rods are [1, 2, 2]. The largest valid frame is [1, 2, 2] because 1 + 2 > 2, giving perimeter 5.

Approach hint

After sorting, the largest possible perimeter should be checked first.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp