<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 judgeSquareSum(int c) {
        int low = 0;
        int high = c;
        int pivot = 0;
        while(low<=high)
        {
            long long mid = low + (high-low)/2;
            if(mid*mid == c) pivot = mid;
            if(mid*mid > c) high = mid-1;
            else low = mid+1;
        }
        pivot = high;
        if(pivot*pivot == c) return true;
        /*vector<int> perfectSquares(pivot);
        for(int i = 1; i<= pivot; i++)
        {
            perfectSquares.push_back(i*i);
        }*/
        low = 1;
        high = pivot;
        while(low<=high)
        {
            long long check = pivot*pivot;
            while(low <= high)
            {
                long long mid = low+(high-low)/2;
                if(mid*mid + check == c) return true;
                else if(mid*mid + check > c) high = mid-1;
                else low = mid+1;
            }
            pivot--;
            low = high;
            high = pivot;
        }
        
        return false;
    }
};
7400