Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1925.html

1925. Count Square Sum Triples

Level

Easy

Description

A square triple (a,b,c) is a triple where a, b, and c are integers and a^2 + b^2 = c^2.

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

Solution

Loop over a, b and c and count the number of square triples.

  • class Solution {
        public int countTriples(int n) {
            int count = 0;
            for (int a = 1; a <= n; a++) {
                for (int b = 1; b <= n; b++) {
                    for (int c = Math.max(a, b) + 1; c <= n; c++) {
                        if (a * a + b * b == c * c)
                            count++;
                    }
                }
            }
            return count;
        }
    }
    
    ############
    
    class Solution {
        public int countTriples(int n) {
            int res = 0;
            for (int a = 1; a <= n; ++a) {
                for (int b = 1; b <= n; ++b) {
                    int t = a * a + b * b;
                    int c = (int) Math.sqrt(t);
                    if (c <= n && c * c == t) {
                        ++res;
                    }
                }
            }
            return res;
        }
    }
    
  • // OJ: https://leetcode.com/problems/count-square-sum-triples/
    // Time: O(N^2)
    // Space: O(1)
    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 cs = a * a + b * b, c = sqrt(cs);
                    if (c > n) break;
                    if (c * c == cs) ++ans;
                }
            }
            return ans;
        }
    };
    
  • class Solution:
        def countTriples(self, n: int) -> int:
            res = 0
            for a in range(1, n + 1):
                for b in range(1, n + 1):
                    t = a**2 + b**2
                    c = int(sqrt(t))
                    if c <= n and c**2 == t:
                        res += 1
            return res
    
    ############
    
    # 1925. Count Square Sum Triples
    # https://leetcode.com/problems/count-square-sum-triples/
    
    class Solution:
        def countTriples(self, n: int) -> int:
            res = 0
            mp = collections.defaultdict(int)
    
            for a in range(1, n + 1):
                for b in range(1, n + 1):
                    mp[a * a + b * b] += 1
            
            for c in range(1, n + 1):
                res += mp[c * c]
    
            
            return res
    
    
  • func countTriples(n int) int {
    	res := 0
    	for a := 1; a <= n; a++ {
    		for b := 1; b <= n; b++ {
    			t := a*a + b*b
    			c := int(math.Sqrt(float64(t)))
    			if c <= n && c*c == t {
    				res++
    			}
    		}
    	}
    	return res
    }
    

All Problems

All Solutions