Valid Perfect Square

Given a positive integer num, return true if num is a perfect square and false otherwise. A perfect square is an integer that equals some integer multiplied by itself, such as 16 = 4 * 4. You must not use any built-in square root function.

30:00

Valid Perfect Square
easy
Free written solution
Topics
Companies

Given a positive integer num, return true if num is a perfect square and false otherwise. A perfect square is an integer that equals some integer multiplied by itself, such as 16 = 4 * 4. You must not use any built-in square root function.

Example 1:
Input: {"num":16}
Output: true
Constraints:
  • 1≤num≤231−11 \leq \text{num} \leq 2^{31} - 1

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"num":16}

Is 16 a perfect square? Its root, if it exists, is somewhere in [1, 16]. Binary search for a side length whose square is exactly 16.

Does a mid x mid square fill num exactly?

dashed = area 16

num

num = 16

mid = -

mid² = -

Candidate sides

all sides 1 .. 16window holds 16 candidateslo 1hi 16
Variables
VariableValue
lo1
hi16
num16
DepthFunction Call
Stack empty
0/5