Problem Solving:: Be Like Hasib from Toph

Tag of this problem is binary search. Binary search problems seems very interesting to me.  Without thinking much about the problem I submitted a simple binary search algorithm. But got wrong answer 5 or 6 times. But after carefully reading the statement I understood I have to make change to my binary search algorithm.
Modified Binary Search::
int count=0;
    ll mid=-1;
    while(l<h)
    {
        mid=(l+h)/2;
        count++;
        //cout<<l<<" "<<h<<" "<<mid<<endl;
        if(x==mid)
        {
            h=mid;
            continue;
        }
        if(x<mid)
            h=mid;
        else l=mid+1;
    }
Then got accepted. it was a nice practice problem.  A good way to start a day.

Comments

Post a Comment

Popular posts from this blog

Java notes Date:26/11/2019

Problem Solving :: Largest Prime from Toph