Implement int sqrt(int x)
.
Compute and return the square root of x.
----------------------
public class Solution {
public int sqrt(int x) {
int left=1, right=x/2;
if(right < left)
return x;
int mid=1;
while(left <= right){
mid = (left+right)/2;
if(x/mid > mid){
left = mid+1;
}
else if(x/mid < mid){
right = mid-1;
}
else
return mid;
}
return right;
}
}