Find first bad version
Solution to Find first bad version problem.
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
if (n < 0)
return n;
if (n==0)
return isBadVersion(0) ? 0 : -1;
int l=0, r=Integer.MAX_VALUE;
int lastBad = -1;
while(l<=r) {
int m = l + (r-l)/2;
if (isBadVersion(m)) { // Check for older bad version.
lastBad=m;
r=m-1;
} else { // versions till m are fine. Check versions greater than m.
l=m+1;
}
}
return lastBad;
}
}