LeetCode Challenge Day 84 — 1925. Count Square Sum Triples
Nitin Ahirwal / December 8, 2025
Hey folks 👋
This is Day 84 of my LeetCode streak 🚀
Today's problem is 1925. Count Square Sum Triples — an easy problem that revisits the classic Pythagorean theorem with clean constraints and a straightforward approach.
📌 Problem Statement
A square triple (a, b, c) is defined such that:
a² + b² = c²
Given an integer n, count how many such ordered triples exist where:
1 ≤ a, b, c ≤ n
Example:
Output: 2
Explanation: (3,4,5) and (4,3,5)
Input: n = 10
Output: 4
Explanation: (3,4,5), (4,3,5), (6,8,10), (8,6,10)`
💡 Intuition
The equation a² + b² = c² directly points to Pythagorean triples.
Since the constraint is small (n ≤ 250), we don’t need any advanced math optimizations.
The key idea is:
- Try all possible values of
aandb - Check if
a² + b²forms a perfect squarec²within range
To make this check fast, we can precompute all squares from 1² to n².
🔑 Approach
- Precompute a set of squares of all numbers from
1ton. - Iterate through all ordered pairs
(a, b)where1 ≤ a, b ≤ n. - Calculate
a² + b²for each pair. - If this value exists in the square set, it means:
a² + b² = c² (for some 1 ≤ c ≤ n)so we count it as a valid square triple. - Return the final count.
Important note:
(a, b, c)and(b, a, c)are treated as different triples, which is why both are counted.
⏱️ Complexity Analysis
| Complexity | Value | |----------|-------| | Time | $$O(n^2)$$ | | Space | $$O(n)$$ |
🧑💻 Code (JavaScript)
/**
* @param {number} n
* @return {number}
*/
var countTriples = function(n) {
// Precompute all squares from 1 to n
const squares = new Set();
for (let c = 1; c <= n; c++) {
squares.add(c * c);
}
let count = 0;
// Check all ordered pairs (a, b)
for (let a = 1; a <= n; a++) {
for (let b = 1; b <= n; b++) {
const sum = a * a + b * b;
if (squares.has(sum)) {
count++;
}
}
}
return count;
};
🎯 Reflection
This problem is a nice reminder that:
-
Constraints guide the solution approach
-
Precomputation can turn expensive checks into constant-time operations
-
Classic math concepts often appear in competitive programming
That’s it for Day 84 of my LeetCode challenge 💪
Streak continues 🔥
Happy Coding 👨💻