<p>Given a non-negative integer <code>c</code>, decide whether there&#39;re two integers <code>a</code> and <code>b</code> such that <code>a<sup>2</sup> + b<sup>2</sup> = c</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> c = 5
<strong>Output:</strong> true
<strong>Explanation:</strong> 1 * 1 + 2 * 2 = 5
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> c = 3
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= c &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution {
public:
    bool isPerfectSquare(int num) {
        if(num == 1){
            return true;
        }
        int l = 1;
        int r = num/2;

        while(l <= r){
            int mid = l + (r-l)/2;
            long long temp = (long long)mid*mid;
            
            if(temp == num){
                return true;
            }
            else if(temp < num){
                l = mid+1;
            }
            else{
                r = mid-1;
            }
        }

        return false;
    }

    int find(int n){
        int l = 1;
        int r = n/2;
        
        while(l <= r){
            int mid = l + (r-l)/2;
            long long temp = (long long)mid*mid;

            if(temp > n){
                r = mid-1;
            }
            else{
                l = mid+1;
            }
        }

        return r;
    }

    bool judgeSquareSum(int c) {
        if(isPerfectSquare(c) || c == 0){
            return true;
        }

        int r = find(c);

        for(int i = 1; i <= r;i++){
            long long temp = (long long)i*i;
            int compliment = c - temp;
            if(isPerfectSquare(compliment)){
                return true;
            }
        }

        return false;
        
    }
};
7200