Valid Perfect Square - Problem

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

A perfect square is an integer that is the square of an integer. In other words, it is the product of some integer with itself.

You must not use any built-in library function, such as sqrt.

Input & Output

Example 1 — Perfect Square
$ Input: num = 16
Output: true
💡 Note: 16 is a perfect square because 4 * 4 = 16
Example 2 — Not Perfect Square
$ Input: num = 14
Output: false
💡 Note: 14 is not a perfect square. √14 ≈ 3.74, which is not an integer
Example 3 — Edge Case
$ Input: num = 1
Output: true
💡 Note: 1 is a perfect square because 1 * 1 = 1

Constraints

  • 1 ≤ num ≤ 231 - 1

Visualization

Tap to expand
Valid Perfect Square ProblemInputnum = 16CheckIs √16 integer?OutputtruePerfect Square Examples1² = 1 ✓2² = 4 ✓3² = 9 ✓4² = 16 ✓5 ✗14 ✗15 ✗
Understanding the Visualization
1
Input
Given positive integer num
2
Process
Find if there exists integer k where k² = num
3
Output
Return true if perfect square, false otherwise
Key Takeaway
🎯 Key Insight: Use binary search to efficiently find the integer square root in O(log n) time
Asked in
Facebook 15 LinkedIn 12
125.0K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen