Problema
Find every triple of positive integers a < b < c for which a² + b² = c² and a + b + c = perimeter. Return each triple as [a, b, c], ordered by increasing a and then b. Do not include permutations of the same sides.
Implement pythagorean_triplets(perimeter).
Input
perimeter: The required sum of all three sides.
Output
Return a list of triples in the specified order.
Restricciones
3 ≤ perimeter ≤ 100,000. Return an empty list if no triangle qualifies.
Ejemplos
Entrada:
perimeter = 30
Salida esperada:
expected = [
[5, 12, 13]
]
Entrada:
long long perimeter = 30;
Salida esperada:
std::vector<std::vector<long long>> expected = {
{5, 12, 13}
};
Entrada:
long perimeter = 30;
Salida esperada:
long[][] expected = new long[][] {
new long[] {5, 12, 13}
};
Explicación
5 + 12 + 13 = 30 and 5² + 12² = 13². This is the only qualifying triple.
Entrada:
perimeter = 11
Salida esperada:
expected = []
Entrada:
long long perimeter = 11;
Salida esperada:
std::vector<std::vector<long long>> expected = {};
Entrada:
long perimeter = 11;
Salida esperada:
long[][] expected = new long[][] {};
Explicación
No increasing positive integer right-triangle triple sums to 11.
Cómo abordarlo
Inténtalo primero por tu cuenta y luego abre las pistas una a una.
Pista 1
A valid triple must satisfy both the Pythagorean equation and the perimeter sum.
Pista 2
Use the perimeter to limit the search instead of trying every ordering of sides.
Pista 3
Remember to keep only a < b < c, and skip duplicates from symmetric side choices.
Complejidad esperada
O(n^2) time, O(1) extra space
Referencias
Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.