/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int l=1,r=n;
while(l<r){
int mid=l+((rl)>>1);
if (!isBadVersion(mid)) l = mid + 1;
else r = mid;
}
return l;
}
}
My java code beats 99.8% 16ms (sometimes)


last + first will larger than the Integer.MAX_VALUE, so the answer will be wrong.U can submit it to check it.
public class Solution extends VersionControl { public int firstBadVersion(int n) { long l=1,r=n; while(l<r){ long mid=(l + r)>>1; if (!isBadVersion((int)mid)) l = mid + 1; else r = mid; } return (int)l; } }