Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Integer Square Root

Searchingeasy

Implement `mySqrt(x)` returning the floor of the square root of a non-negative integer `x`, computed with binary search.

Sample tests

Input: mySqrt(4)
Output: 2
Input: mySqrt(8)
Output: 2

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • mid * mid can be compared safely here, but watch the loop bounds.

Learning resources

  • Wikipedia: Binary search
Approach & explanation (try first)

Binary searching the integer square root runs in O(log x) time.

Loading...
⌘/Ctrl + Enter

Run your code to see results.