Following is my solution for the problem statement first bad version.
For my solution, firstBadVersion is always correct (Checked using LeetCode #278).
However, it reports (expectedCount - 1) API calls for certain test cases.
Need help with it.
public class FBVersion{
// -- DO NOT CHANGE THIS SECTION -----------------
public static boolean isBadVersion(int v){ // isBadVersion() is the API function that returns true or false depending upon whether the provided version ID is bad or not
return Main.isBad(v);
}
//-------------------------------------------------
public static int[] firstBadVersion(int n) {
int left = 1;
int right = n;
int nCalls = 1;
while(left <= right) {
int mid = (int) Math.floor((left+right)/2);
boolean isBad = isBadVersion(mid);
if(!isBad) {
left = mid + 1;
}
else if(isBad && isBadVersion(mid - 1)){
right = mid - 1;
}
else {
return new int[]{mid, nCalls};
}
nCalls++;
}
return new int[]{0, 0};
}
}