Count Square Sum Triples (LeetCode 1925): Solution and Explanation

Easy
Math
Enumeration

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

  1. Set count = 0.
  2. For each a from 1 to n, loop b from a + 1 to n.
  3. Compute s = a² + b². If s > n², break out of the b loop.
  4. Let c = floor(sqrt(s)). If c * c == s, add 2 to count.
  5. Return count.

Step-by-step example

n = 5
  1. a = 1: b = 2, 3, 4 give 5, 10, 17, none of them squares. b = 5 gives 26 > 25, so the row stops.
  2. a = 2: b = 3, 4 give 13 and 20, not squares. b = 5 gives 29 > 25, stop.
  3. a = 3: b = 4 gives 25 = 5². Count (3, 4, 5) and (4, 3, 5): count = 2. b = 5 gives 34 > 25, stop.
  4. 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 count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Euclid's formula O(n log n)
Enumerate pairs with an early break O(n²)
Set of squares O(n²)
Triple loop O(n³)
Time

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²).

Space

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.