<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) 
    {
        if (sqrt(c) == ceil(sqrt(c))) return true;
       
        // Take advantage of the fact the number line is sorted so you can se two pointers
        /* long left = 1;
        long right = (long)sqrt(c);
        while (left <= right) {
            long sum = left*left + right*right;

            if (sum == c) {
                return true;
            }
            else if (sum < c) {
                left++;
            }
            else {
                right--;
            }
        }

        return false;*/

        // Can also use two sum technique, but this will not be constant space
        unordered_set <int> squares;

        for (long i=0; i <= sqrt(c); i++) {
            if (squares.find(c-i*i) != squares.end()) return true;
            if ((2*(i*i)) == c) return true;
            squares.insert(i*i);
        }

        return false;
    }
};
73600