samedi 23 avril 2016

Binary Search to find maximal minimum value

I am trying to find the maximal minimum value the I can places some numbers apart in an interval. Also in the interval there is a gap, based on the input.

while(high-low>0)
{
    mid=(high+low)/2.0;
    distance=0;

    for(int posts=1; posts<p; posts++)
    {
        //the gap in the interval 
        if( distance+mid>a && distance+mid<b)
        {
           //if the digits falls between the gap 
           //continue the search from the edge of the gap's end
            distance = b;
            posts++;
        }
        distance += mid;
        if(distance>length)break;

    }
    if(distance==length)return mid;
    else if(distance<length)low=mid;
    else high=mid;
}

The problem I have is that for some values, this will not work. The distance will go close to the length but it will not be equal. I tried rounding the distance but then the value will not be the correct one.

Aucun commentaire:

Enregistrer un commentaire