Problem statement

https://leetcode.com/problems/sum-of-square-numbers/

Solution

I am not sure, why this problem is marked as medium: all you need to do is to do bruteforce: just consider all numbers before sqrt(c) and check if corresponding b is integer. Why we need to check all numbers only before sqrt(c)? Because square is always not-negative and if first part is greater than c, we can not make sum equal to c.

Complexity

Time complexity is O(sqrt(c)), space complexity is O(1).

Code

class Solution:
    def judgeSquareSum(self, c):
        for i in range(int(math.sqrt(c))+1):
            j = c - i*i
            if int(math.sqrt(j))**2 == j: return True
        return False