1925. Count Square Sum Triples LeetCode Solution
In this guide, you will get 1925. Count Square Sum Triples LeetCode Solution with the best time and space complexity. The solution to Count Square Sum Triples problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Square Sum Triples solution in C++
- Count Square Sum Triples solution in Java
- Count Square Sum Triples solution in Python
- Additional Resources
Problem Statement of Count Square Sum Triples
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
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n)
1925. Count Square Sum Triples LeetCode Solution in C++
class Solution {
public:
int countTriples(int n) {
int ans = 0;
unordered_set<int> squared;
for (int i = 1; i <= n; ++i)
squared.insert(i * i);
for (const int a : squared)
for (const int b : squared)
if (squared.contains(a + b))
++ans;
return ans;
}
};
/* code provided by PROGIEZ */
1925. Count Square Sum Triples LeetCode Solution in Java
class Solution {
public int countTriples(int n) {
int ans = 0;
Set<Integer> squared = new HashSet<>();
for (int i = 1; i <= n; ++i)
squared.add(i * i);
for (final int a : squared)
for (final int b : squared)
if (squared.contains(a + b))
++ans;
return ans;
}
}
// code provided by PROGIEZ
1925. Count Square Sum Triples LeetCode Solution in Python
class Solution:
def countTriples(self, n: int) -> int:
ans = 0
squared = set()
for i in range(1, n + 1):
squared.add(i * i)
for a in squared:
for b in squared:
if a + b in squared:
ans += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.