Sqrt(x)
Sqrt(x) is LeetCode 69 (Easy). Given a non-negative integer x, return its square root rounded down to a whole number.
- Built-in power and root functions (
pow,x ** 0.5,sqrt) are not allowed. xgoes up to 2³¹ − 1, so the square of a guess can be far larger than a 32-bit integer holds.
What you are asked for is the integer square root: the largest whole number whose square is not more than x.
- 0 <= x <= 2³¹ - 1
Intuition
Squares only grow: 0, 1, 4, 9, 16, 25 … So one guess m tells you which side the answer is on.
- The guess fits:
m * m <= x. Thenmcould be the answer, but a bigger number might fit too, so keepmand look right. - The guess is too big:
m * m > x. Thenmand every number above it are out, so look left.
That is binary search over the candidate answers 0 to x, not over an array: every guess halves the range. Most x are not perfect squares, so no guess lands exactly. The search keeps the last guess that fit, and that is the root rounded down.
"Find the largest number whose value passes a test", where the test only gets harder as the number grows, is binary search on the answer. Nth roots, Koko's eating speed and ship capacity are the same search with a different test.
Approach
Before reading on, pick a range that surely holds the answer, and decide what one guess tells you when its square is too big, and when it fits. Then decide which number you return when no guess squares to exactly x.
Set the range of candidates
Start with lo = 0, hi = x and ans = 0. The root of a non-negative whole number is never bigger than the number itself, so every possible answer lies in this range.
Square the middle guess
Take mid halfway between lo and hi and compute mid * mid. In C++ and Java, do the multiplication in a 64-bit type (long long, long), because the first guesses for a large x are around a billion.
Keep a guess that fits and look right
If the square is at most x, store ans = mid and move lo = mid + 1. mid is a valid answer so far, and only a larger number could be a better one.
Drop a guess that is too big and look left
Otherwise move hi = mid - 1. Every number from mid upward squares past x, so none of them can be the answer.
Return the last guess that fit
The loop ends when lo passes hi. By then every number up to ans has a square at most x and every number above it does not, so ans is the largest whole number whose square fits.
Code in Python, C++ and Java
Common pitfalls
Squaring the guess in a 32-bit int
int mid = lo + (hi - lo) / 2; if (mid * mid <= x)
int mid = lo + (hi - lo) / 2; if ((long long)mid * mid <= x)
For x = 2147395599 the first guess is mid = 1073697799, whose square is about 1.15 × 10¹⁸. An int cannot hold that, so the product wraps to a meaningless value that can look smaller than x, and the search heads the wrong way. Python integers never overflow, so only C++ and Java hit this.
Returning lo after the loop
return lo
return ans # or hi
When the loop stops, lo sits one past the last guess that fit. For x = 8 it ends with lo = 3 and hi = 2, and 3 × 3 = 9 is already too big. The saved ans (equal to hi here) is the correct 2.
Edge cases
x = 0The range is just 0 to 0. The only guess, 0, fits, so the answer is 0 with no special case.
x = 2147483647, the largest inputThe answer is 46340: its square, 2,147,395,600, fits, while 46341² = 2,147,488,281 is too big. The search needs about 31 rounds.
Complexity
x needs at most about 32 rounds. Only three numbers are stored.Sqrt(x) FAQ
Is Newton's method faster than binary search here?
Usually, yes. Start with r = x and repeat r = (r + x // r) // 2 while r * r > x; with whole-number division it stops exactly on the rounded-down root. It needs far fewer rounds for big x, but it is harder to argue correct in an interview, so binary search is the common answer.
Why not call sqrt() and round down?
The problem bans it. Outside the problem, a double root is exact enough for every 32-bit x, but for 64-bit inputs it can be off by one near perfect squares, which is why whole-number methods matter.
How is Valid Perfect Square different?
Valid Perfect Square (LeetCode 367) runs the same search but answers yes or no: it returns true only if some guess squares to exactly num, instead of keeping the last guess that fit.