Math Fundamentals18 sections · 814 units
Open in Course

Practice - Sum of Square Numbers

LeetCode 633

Given a non-negative integer cc, decide whether there exist two integers aa and bb such that a2+b2=ca^2 + b^2 = c.

For example, c=5c = 5 returns true because 12+22=1+4=51^2 + 2^2 = 1 + 4 = 5. But c=3c = 3 returns false.

Hint: use two pointers starting at a=0a = 0 and b=cb = \lfloor \sqrt{c} \rfloor. Move pointers based on whether a2+b2a^2 + b^2 is less than, equal to, or greater than cc.