Problema
Find positive integers a < b < c with a² + b² = c² and a + b + c = perimeter. Return the largest product a × b × c among qualifying triangles, or −1 if there are none. This generalizes the fixed-perimeter source task and explicitly handles multiple answers.
Implement pythagorean_product(perimeter).
Input
perimeter: The required sum of the three sides.
Output
Return the largest product, or −1 when no triangle qualifies.
Restricciones
3 ≤ perimeter ≤ 100,000. Products fit signed 64-bit integers.
Ejemplos
Entrada:
perimeter = 30
Salida esperada:
expected = 780
Entrada:
long long perimeter = 30;
Salida esperada:
long long expected = 780;
Entrada:
long perimeter = 30;
Salida esperada:
long expected = 780;
Explicación
The triangle is (5, 12, 13): 25 + 144 = 169 and 5 + 12 + 13 = 30. Its product is 780.
Entrada:
perimeter = 11
Salida esperada:
expected = -1
Entrada:
long long perimeter = 11;
Salida esperada:
long long expected = -1;
Entrada:
long perimeter = 11;
Salida esperada:
long expected = -1;
Explicación
No increasing positive integer right-triangle sides sum to 11.
Cómo abordarlo
Inténtalo primero por tu cuenta y luego abre las pistas una a una.
Pista 1
Only certain perimeters can form a right triangle, so first narrow the search using the side-sum constraint.
Pista 2
For each possible shortest side, derive the other two sides from the perimeter and check the right-triangle condition.
Pista 3
More than one triangle may fit; keep the largest product and return no-solution when nothing valid appears.
Complejidad esperada
O(perimeter^2) time, O(1) extra space
Referencias
Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.