Find the square root of a number,Use binary search

long long int floorSqrt(long long int x)
{
// Your code goes here
    int lo=0,hi=1000,mid;
    while(hi-lo>=1)
    {
        mid=(lo+hi)/2;
        if(mid*mid<x)
        lo=mid+1;
        else if(mid*mid>x)
        hi=mid-1;
        else
        return mid;
    }
    if(lo*lo>x)
    return lo-1;
    else
    return lo;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *