This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

Given a non-negative integer `c`

, your task is to decide whether there're two integers `a`

and `b`

such that a^{2} + b^{2} = c.

**Example 1:**

Input:5Output:TrueExplanation:1 * 1 + 2 * 2 = 5

**Example 2:**

Input:3Output:False

b'

\n## Solution

\n

\n#### Approach #1 Brute Force [Time Limit Exceeded]

\n

\n#### Approach #2 Better Brute Force [Time Limit Exceeded]

\n

\n#### Approach #3 Using sqrt function [Accepted]

\n

\n#### Approach #4 Using Binary Search [Accepted]

\n

\n#### Approach #5 Fermat Theorem [Accepted]:

\n

\n

'
\n\n

\n\n

The simplest solution would be to consider every possible combination of integers and and check if the sum of their squares equals . Now, both and can lie within the range . Thus, we need to check for the values of and in this range only.

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . Two loops upto . Here, refers to the given integer(sum of squares).

\n \n - \n
Space complexity : . Constant extra space is used.

\n \n

\n

We can improve the last solution, if we make the following observation. For any particular chosen, the value of required to satisfy the equation will be such that . Thus, we need to traverse over the range only for considering the various values of . For every current value of chosen, we can determine the corresponding value and check if it is a perfect square or not. If it happens to be a perfect square, is a sum of squares of two integers, otherwise not.

\nNow, to determine, if the number is a perfect square or not, we can make use of the following theorem: "The square of positive integer can be represented as a sum of first odd positive integers." Or in mathematical terms:

\n\n.

\nTo look at the proof of this statement, look at the L.H.S. of the above statement.

\n\n\n

\n\n\n

\n\n\n

\n\n\n

\n\n\n

\n\n\n

\nThis completes the proof of the above statement.

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . The total number of times the is updated is: .

\n \n - \n
Space complexity : . Constant extra space is used.

\n \n

\n

**Algorithm**

Instead of finding if is a perfect square using sum of odd numbers, as done in the last approach, we can make use of the inbuilt function and check if turns out to be an integer. If it happens for any value of in the range , we can return a True value immediately.

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . We iterate over values for choosing . For every chosen, finding square root of takes time in the worst case.

\n \n - \n
Space complexity : . Constant extra space is used.

\n \n

\n

**Algorithm**

Another method to check if is a perfect square, is by making use of Binary Search. The method remains same as that of a typical Binary Search to find a number.\nThe only difference lies in that we need to find an integer, in the range , such that this number is the square root of .\nOr in other words, we need to find an integer, , in the range , such that x.

\nThe following animation illustrates the search process for a particular value of .

\n!?!../Documents/633_Sum_of_Squares.json:1000,563!?!

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . Binary search taking in the worst case is done for values of .

\n \n - \n
Space complexity : . Binary Search will take space.

\n \n

\n

**Algorithm**

This approach is based on the following statement, which is based on Fermat\'s Theorem:

\n"Any positive number is expressible as a sum of two squares if and only if the prime factorization of , every prime of the form occurs an even number of times."

\nBy making use of the above theorem, we can directly find out if the given number can be expressed as a sum of two squares.

\nTo do so we simply find all the prime factors of the given number , which could range from along with the count of those factors, by repeated division. \nIf at any step, we find out that the number of occurences of any prime factor of the form occurs an odd number of times, we can return a False value.

\nIn case, itself is a prime number, it won\'t be divisible by any of the primes in the . Thus, we need to check if can be expressed in the form of\n. If so, we need to return a False value, indicating that this prime occurs an odd number(1) of times.

\nOtherwise, we can return a True value.

\nThe proof of this theorem includes the knowledge of advanced mathematics and is beyond the scope of this article. However, interested reader can refer to this documentation.

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . We find the factors of and their count using repeated division. We check for the factors in the range .\nThe maximum number of times a factor can occur(repeated division can be done) is (considering 2 as the only factor, . Thus, ).

\n \n - \n
Space complexity : . Constant space is used.

\n \n

\n

Analysis written by: @vinod23

\n