Skip to main content

Count Square Sum Triples - Solution & Explanation

EasyMathEnumeration14 min readAsked at: Meta, Google, Turing +2
Practice this problem

Problem Statement

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 <= 250

Approach Overview

Problem Overview: Given an integer n, count the number of ordered triples (a, b, c) such that 1 ≤ a, b, c ≤ n and a² + b² = c². The task is essentially finding all Pythagorean triples within the range [1, n].

Approach 1: Brute Force Enumeration (O(n³) time, O(1) space)

Iterate over every possible combination of a, b, and c from 1 to n. For each triple, compute a*a + b*b and compare it with c*c. If they match, increment the count. This method directly checks the Pythagorean condition without any optimization. It’s useful for understanding the problem constraints and validating smaller inputs, but the cubic time complexity becomes expensive as n grows.

Approach 2: Optimized Loop with Square Root Check (O(n²) time, O(1) space)

Instead of iterating over all three variables, iterate only over a and b. Compute sum = a*a + b*b and check whether it forms a perfect square. If c = sqrt(sum) is an integer and c ≤ n, a valid triple exists. This removes the third loop and reduces the complexity to quadratic. The key insight is treating c as a derived value rather than iterating through all candidates. This approach relies on basic math properties and systematic enumeration of pairs.

Recommended for interviews: Start by describing the brute force enumeration to show you understand the constraint a² + b² = c². Then move to the optimized two-loop approach that computes c using a square root check. Interviewers typically expect the O(n²) solution because it removes unnecessary iteration while keeping the implementation simple.

Approach 1: Brute Force Approach

The brute force approach involves iterating through all possible values of a, b, and c within the range of 1 to n and checking if they satisfy the condition a^2 + b^2 = c^2.

This solution uses three nested loops to iterate over all values of a, b, and c from 1 to n. It checks if the sum of the squares of a and b equals the square of c. If a valid triple is found, the count is incremented. The result is the number of such valid triples.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^3)
Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Optimized Loop Approach

By noticing that c will always be larger than or equal to both a and b in a Pythagorean triple, we can optimize by directly iterating a and b with the constraint that their sum of squares should exactly form a square of another integer c.

This optimized C solution uses only two nested loops over a and b, calculating c using the square root of a^2 + b^2. If c is an integer and within bounds, it counts both (a, b, c) and (b, a, c) as valid triples due to symmetry, thus incrementing count by 2.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2)
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Enumeration

We enumerate a and b in the range [1, n), then calculate c = \sqrt{a^2 + b^2}. If c is an integer and c leq n, then we have found a Pythagorean triplet, and we increment the answer by one.

After the enumeration is complete, return the answer.

The time complexity is O(n^2), where n is the given integer. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Brute Force Approach

Time Complexity: O(n^3)
Space Complexity: O(1)

Optimized Loop Approach

Time Complexity: O(n^2)
Space Complexity: O(1)

Enumeration

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n³)O(1)When demonstrating the direct definition of the problem or validating small constraints
Optimized Loop with Square Root CheckO(n²)O(1)Preferred approach for interviews and production due to reduced iteration

Video Solution

Count Square Sum Triples | Simple and Easy Solution | Leetcode 1925 | codestorywithMIKcodestorywithMIK4,161 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Count Square Sum Triples easy or hard?
Count Square Sum Triples is classified as an Easy problem on LeetCode with an acceptance rate above 70%. The challenge mainly involves recognizing the Pythagorean relation a² + b² = c² and optimizing the brute force enumeration.
Count Square Sum Triples Python/Java solution
Most implementations use two nested loops and a square root check. Python solutions typically use math.sqrt while Java implementations use Math.sqrt and verify that the result is an integer before counting the triple.
How to solve Count Square Sum Triples in O(n²)?
Loop through all pairs (a, b) from 1 to n. For each pair, compute sum = a² + b² and calculate c = sqrt(sum). If c is an integer and c ≤ n, increment the count. This avoids the third loop and keeps the algorithm at O(n²) time.
What is the best approach for Count Square Sum Triples?
The optimized enumeration approach using two loops is the best solution. Iterate over values of a and b, compute a² + b², and check if the result is a perfect square c where c ≤ n. This reduces the complexity to O(n²) with O(1) space.
Is Count Square Sum Triples asked at Google/Amazon/Meta?
This problem is categorized as Easy and focuses on math-based enumeration. Variations of Pythagorean triple checks and enumeration problems appear in interviews at companies like Amazon and Google to test basic algorithmic reasoning.
What data structure is used in Count Square Sum Triples?
No advanced data structure is required. The problem primarily uses mathematical computation and simple loops. Some implementations optionally use a set of squares for faster lookups, but the standard optimized solution works with constant extra space.
What is the time complexity of Count Square Sum Triples?
The brute force approach runs in O(n³) time because it checks every combination of a, b, and c. The optimized solution reduces this to O(n²) by computing c directly from a² + b² and verifying whether it is a valid integer within range.

Ready to solve this problem?

Practice Count Square Sum Triples with our built-in code editor and test cases.

Practice on FleetCode