A mosaic artisan has exactly N identical square tiles and wants to lay them out on the floor as one solid k x k square, edge to edge, with no leftover tiles and no gaps to fill. That is only possible when N is itself a perfect square (N = k * k for some positive integer k).
Given N, decide whether the artisan's tiles can form such a square, without using any built-in square-root, power, or logarithm function - use an integer-only technique such as narrowing down the candidate side length by comparison (for example, binary search) instead.
A single line containing one integer N.
Print YES if N is a perfect square, otherwise print NO.
1 <= N <= 2147483647 (fits in a 32-bit signed integer)Example 1
Input
16
Expected
YES
Explanation
16 = 4 * 4, so 16 tiles can be arranged into a solid 4x4 square with nothing left over. The answer is YES.
Example 2
Input
14
Expected
NO
Explanation
3*3 = 9 and 4*4 = 16, and 14 falls strictly between them, so there is no integer k with k*k = 14. The answer is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →