Sqrt(x)
Sqrt(x) is a easy binary search problem solved with the search on answer range pattern.
The best approach, optimal (binary search for the last true), runs in O(log x) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from linear search up.
Problem
Given a non-negative integer x, return its square root rounded down to an integer, without using a built-in power or square root function.
Examples
Example 1
- Input
x = 16- Output
4
Example 2
- Input
x = 20- Output
4- Why
- sqrt(20) ≈ 4.47, rounded down.
Constraints
0 <= x <= 2^31 - 1- No built-in pow or sqrt.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 14 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Linear search | O(√x) | O(1) |
| Optimal (binary search for the last true) | O(log x) | O(1) |
1Linear search
O(√x)O(1)Increase r while (r + 1)^2 <= x.
- r = 0; while (r + 1)^2 <= x: r++.
class Solution {
public int mySqrt(int x) {
long r = 0;
while ((r + 1) * (r + 1) <= x) r++;
return (int) r;
}
}class Solution:
def mySqrt(self, x: int) -> int:
r = 0
while (r + 1) * (r + 1) <= x:
r += 1
return rclass Solution {
public:
int mySqrt(int x) {
long long r = 0;
while ((r + 1) * (r + 1) <= x) r++;
return (int)r;
}
};function mySqrt(x) {
let r = 0;
while ((r + 1) * (r + 1) <= x) r++;
return r;
}2Optimal (binary search for the last true)
O(log x)O(1)r · r <= x is true for small r and false for large r. Binary search for the last r where it is true, using long for the square.
- lo = 0, hi = x.
- If mid * mid <= x: ans = mid, lo = mid + 1. Else hi = mid - 1.
class Solution {
public int mySqrt(int x) {
long lo = 0, hi = x, ans = 0;
while (lo <= hi) {
long mid = (lo + hi) / 2;
if (mid * mid <= x) { ans = mid; lo = mid + 1; }
else hi = mid - 1;
}
return (int) ans;
}
}class Solution:
def mySqrt(self, x: int) -> int:
lo, hi, ans = 0, x, 0
while lo <= hi:
mid = (lo + hi) // 2
if mid * mid <= x:
ans = mid
lo = mid + 1
else:
hi = mid - 1
return ansclass Solution {
public:
int mySqrt(int x) {
long long lo = 0, hi = x, ans = 0;
while (lo <= hi) {
long long mid = (lo + hi) / 2;
if (mid * mid <= x) { ans = mid; lo = mid + 1; }
else hi = mid - 1;
}
return (int)ans;
}
};function mySqrt(x) {
let lo = 0, hi = x, ans = 0;
while (lo <= hi) {
const mid = Math.floor((lo + hi) / 2);
if (mid * mid <= x) { ans = mid; lo = mid + 1; }
else hi = mid - 1;
}
return ans;
}Edge cases to test
- x = 0 and x = 1
- mid * mid overflows int; use long
Hints
Hint 1
Find the largest r with r · r <= x.
FAQ
What is the best time complexity for Sqrt(x)?
Optimal (binary search for the last true) runs in O(log x) time and O(1) extra space.
Which pattern does Sqrt(x) use?
It is a binary search problem that uses the search on answer range pattern. Other problems with the same pattern: Pow(x, n) (Binary Exponentiation), Find Nth root of M, Find position of an element in a sorted array of infinite numbers (Galloping Search).
Is there a brute force solution for Sqrt(x)?
Yes. Linear search takes O(√x) time and O(1) space. Increase r while (r + 1)^2 <= x.
Which edge cases should I test for Sqrt(x)?
x = 0 and x = 1; mid mid overflows int; use long.