Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
A square triple (a,b,c) is a triple where a, b, and c are integers and a2 + b2 = c2.
Given an integer n, return the number of square triples such that 1 <= a, b, c <= n.
Example 1:
Input: n = 5 Output: 2 Explanation: The square triples are (3,4,5) and (4,3,5).
Example 2:
Input: n = 10 Output: 4 Explanation: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10).
Constraints:
1 <= n <= 250Problem summary: A square triple (a,b,c) is a triple where a, b, and c are integers and a2 + b2 = c2. Given an integer n, return the number of square triples such that 1 <= a, b, c <= n.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
5
10
number-of-unequal-triplets-in-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1925: Count Square Sum Triples
class Solution {
public int countTriples(int n) {
int ans = 0;
for (int a = 1; a < n; a++) {
for (int b = 1; b < n; b++) {
int x = a * a + b * b;
int c = (int) Math.sqrt(x);
if (c <= n && c * c == x) {
ans++;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #1925: Count Square Sum Triples
func countTriples(n int) (ans int) {
for a := 1; a < n; a++ {
for b := 1; b < n; b++ {
x := a*a + b*b
c := int(math.Sqrt(float64(x)))
if c <= n && c*c == x {
ans++
}
}
}
return
}
# Accepted solution for LeetCode #1925: Count Square Sum Triples
class Solution:
def countTriples(self, n: int) -> int:
ans = 0
for a in range(1, n):
for b in range(1, n):
x = a * a + b * b
c = int(sqrt(x))
if c <= n and c * c == x:
ans += 1
return ans
// Accepted solution for LeetCode #1925: Count Square Sum Triples
struct Solution;
impl Solution {
fn count_triples(n: i32) -> i32 {
let mut res = 0;
for a in 1..n {
for b in a + 1..n {
for c in b + 1..=n {
if a * a + b * b == c * c {
res += 1;
}
}
}
}
2 * res
}
}
#[test]
fn test() {
let n = 5;
let res = 2;
assert_eq!(Solution::count_triples(n), res);
let n = 10;
let res = 4;
assert_eq!(Solution::count_triples(n), res);
}
// Accepted solution for LeetCode #1925: Count Square Sum Triples
function countTriples(n: number): number {
let ans = 0;
for (let a = 1; a < n; a++) {
for (let b = 1; b < n; b++) {
const x = a * a + b * b;
const c = Math.floor(Math.sqrt(x));
if (c <= n && c * c === x) {
ans++;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.