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.
Restrições
3 ≤ perimeter ≤ 100,000. Return an empty list if no triangle qualifies.
Exemplos
Entrada:
perimeter = 30
Saída esperada:
expected = [
[5, 12, 13]
]
Entrada:
long long perimeter = 30;
Saída esperada:
std::vector<std::vector<long long>> expected = {
{5, 12, 13}
};
Entrada:
long perimeter = 30;
Saída esperada:
long[][] expected = new long[][] {
new long[] {5, 12, 13}
};
Explicação
5 + 12 + 13 = 30 and 5² + 12² = 13². This is the only qualifying triple.
Entrada:
perimeter = 11
Saída esperada:
expected = []
Entrada:
long long perimeter = 11;
Saída esperada:
std::vector<std::vector<long long>> expected = {};
Entrada:
long perimeter = 11;
Saída esperada:
long[][] expected = new long[][] {};
Explicação
No increasing positive integer right-triangle triple sums to 11.
Como abordar
Tente sozinho primeiro e depois abra uma dica de cada vez.
Dica 1
A valid triple must satisfy both the Pythagorean equation and the perimeter sum.
Dica 2
Use the perimeter to limit the search instead of trying every ordering of sides.
Dica 3
Remember to keep only a < b < c, and skip duplicates from symmetric side choices.
Complexidade esperada
O(n^2) time, O(1) extra space
Referências
Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.