Problem 858
Sum of Square Numbers
Medium · 20 pts · Math, Two Pointers
Problem
Decide whether a non-negative integer C can be written as A² + B² for some non-negative integers A and B (they may be equal, and either may be 0). Use two pointers: start with A = 0 and B = floor(√C); if A² + B² is too small increase A, if it is too large decrease B, and stop when they cross. Use exact integer square roots to avoid floating-point mistakes.
Input
A single integer C.
Output
Print YES if C is a sum of two squares, otherwise NO.
Constraints
0 <= C <= 2^31 - 1
Example input
5
Example output
YES
Explanation
5 = 1² + 2², so the answer is YES. For C = 3 no pair of squares adds up to 3, so it would be NO.