Count Square Sum Triples (LeetCode 1925): Solution and Explanation
Count the ordered triples (a, b, c) with a² + b² = c² and every value between 1 and n.
Intuition
Once a and b are chosen, c is forced: it has to be the square root of a² + b². So the search is over pairs, not triples.
Only pairs with a² + b² <= n² can produce c <= n. In the a-b plane those pairs sit inside a quarter circle of radius n, and every row can stop as soon as it leaves that circle.
a = b never works, because 2a² lies strictly between two consecutive squares. So scan only a < b and count each hit twice, once for (a, b, c) and once for (b, a, c).
Approach: Enumerate pairs with an early break
- Set count = 0.
- For each a from 1 to n, loop b from a + 1 to n.
- Compute s = a² + b². If s > n², break out of the b loop.
- Let c = floor(sqrt(s)). If c * c == s, add 2 to count.
- Return count.
Step-by-step example
n = 5
- a = 1: b = 2, 3, 4 give 5, 10, 17, none of them squares. b = 5 gives 26 > 25, so the row stops.
- a = 2: b = 3, 4 give 13 and 20, not squares. b = 5 gives 29 > 25, stop.
- a = 3: b = 4 gives 25 = 5². Count (3, 4, 5) and (4, 3, 5): count = 2. b = 5 gives 34 > 25, stop.
- a = 4: b = 5 gives 41 > 25, stop. a = 5 has no b > a within 5. Return 2.
Code: Python, Java, C++
1class Solution:
2 def countTriples(self, n: int) -> int:
3 count = 0
4 for a in range(1, n + 1):
5 for b in range(a + 1, n + 1):
6 c2 = a * a + b * b
7 if c2 > n * n:
8 break
9 c = int(c2 ** 0.5)
10 if c * c == c2:
11 count += 2
12 return count1class Solution {
2 public int countTriples(int n) {
3 int count = 0;
4 for (int a = 1; a <= n; a++) {
5 for (int b = a + 1; b <= n; b++) {
6 int c2 = a * a + b * b;
7 if (c2 > n * n) {
8 break;
9 }
10 int c = (int) Math.sqrt(c2);
11 if (c * c == c2) {
12 count += 2;
13 }
14 }
15 }
16 return count;
17 }
18}1class Solution {
2public:
3 int countTriples(int n) {
4 int count = 0;
5 for (int a = 1; a <= n; a++) {
6 for (int b = a + 1; b <= n; b++) {
7 int c2 = a * a + b * b;
8 if (c2 > n * n) {
9 break;
10 }
11 int c = (int) sqrt(c2);
12 if (c * c == c2) {
13 count += 2;
14 }
15 }
16 }
17 return count;
18 }
19};Time and space complexity
O(n²)
The scanned pairs fill about one eighth of the n x n square (a < b inside the quarter circle), which is still Θ(n²).
O(1)
Only the loop variables and the counter are stored.
Other approaches
Set of squares (time O(n²), space O(n))
Put every c² for c in 1..n into a hash set, then count ordered pairs (a, b) whose sum of squares is in the set. Same O(n²) time, no square roots, but O(n) extra memory.
Euclid's formula (time O(n log n), space O(1))
Generate primitive triples (m² - k², 2mk, m² + k²) for coprime m > k of opposite parity and add all their multiples with c <= n. Much faster for large n, but more code than this problem needs.
Triple loop (time O(n³), space O(1))
Try every a, b and c. Correct but O(n³), about 15 million checks for n = 250.
Common mistakes
- Counting each unordered pair once. The problem treats (3, 4, 5) and (4, 3, 5) as different triples.
- Forgetting the c <= n limit. 5² + 12² = 13², which is a valid triple only when n >= 13.
- Comparing sqrt(s) to its rounded value with floating point. Use an integer root and check c * c == s.
- Breaking out of the outer loop instead of the inner one. Leaving the circle in one row says nothing about the next row, which starts again at a small b.
Frequently asked questions
Why can a never equal b?
Then c² = 2a², so c = a·√2, which is irrational. No whole number c works.
Why is the answer always even?
Every triple (a, b, c) has a mirror (b, a, c), and since a is never equal to b the two are distinct. The scan finds one of each pair and adds 2.
Is the early break needed?
Not for correctness, since c <= n can be checked directly. It only skips pairs that cannot work, roughly halving the work for this problem.