Implement int sqrt(int x)
.
Compute and return the square root of x.
Subscribe to see which companies asked this question
class Solution {
public:
int mySqrt(int x) {
int l = 1, r=x;
while(l <= r) {
int mid = (l+r)/2;
if(mid <= x/mid){
l = mid+1;
} else {
r = mid-1;
}
}
return l-1;
}
};