DEV Community

Vishal Yadav
Vishal Yadav

Posted on

First bad version problem

Solution:

// The API isBadVersion is defined for you.
// bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {

         long long start=0,end=n,mid;

       while(end-start>1)
       { 
           mid=(start+end)/2;

        if(isBadVersion(mid))
            end=mid;

        else
            start=mid;
       }
        return end;
    }
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)