456. Valid Perfect Square (BS Approach)

EasyBinary SearchMathBinary Search

Given a positive integer num, return true if it is a perfect square, false otherwise. A perfect square is an integer that is the square of another integer. Do not use built-in sqrt functions. Solve in O(log n).

Input: A positive integer num.

Output: true if num is a perfect square, false otherwise.

Examples

Example 1
Input: 16
Output: true
Explanation: 4*4=16.
Example 2
Input: 14
Output: false
Explanation: No integer r with r*r=14.
Example 3
Input: 1
Output: true
Explanation: 1*1=1.

Constraints

Asked by

HCLTechMicrosoftBloombergGoogleMetaAmazon
Solve this problem in the editor →