All problems

Valid Perfect Square

Easy · binary search

A positive integer is a perfect square if it equals r * r for some whole number r. For example 1, 4, 9 and 16 are perfect squares, but 2, 14 and 20 are not.

Given a positive integer num, return true if it is a perfect square and false otherwise.

Do not use a built-in square root, power or floating-point function.

Examples

Example 1

Input: num = 16

Output: true

4 * 4 = 16.

Example 2

Input: num = 14

Output: false

3 * 3 = 9 is too small and 4 * 4 = 16 is too big, so no whole number squares to 14.

Example 3

Input: num = 1

Output: true

Constraints