Sqrt(x)
August 20, 2016
Just the used the binary search algorithm to find the sqrt. Had to account for large numbers, so I used long instead of int.
Full Solution in Java:
public class Solution { public int mySqrt(int x) { int low = 0; int high = x/2+1; long mid = (low+high)/2; while(low<=high){ long test = mid*mid; if(test==(long)x){ return (int)mid; } if(test<(long)x){ low = (int)mid+1; mid = (low+high)/2; } else if(test>(long)x){ high = (int)mid-1; mid = (low+high)/2; } } return (int)mid; } }