Sqrt(x) using Binary SeARCH

//Square root using Binary Search is showing time limit exceeded for input 2147395599
int mySqrt(int x) {

if (x == 0 || x == 1) 
   return x;
int start = 0, end = x/2, ans;   
while (start <= end) 
{        
    int mid = (start + end) / 2;
    if (mid*mid == x)
        return mid;
    if (mid*mid < x) 
    {
        start = mid + 1;
        ans = mid;
    } 
    else 
        end = mid - 1;        
}
return ans;

}

Comments (4)