-
Notifications
You must be signed in to change notification settings - Fork 0
/
BadVersion.java
35 lines (29 loc) · 1.1 KB
/
BadVersion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
class Solution extends VersionControl {
public int firstBadVersion(int n) {
int first=1,last=n,middle=0;
boolean out=false;
while(first<=last){
/**
*
* Here we have calculated middle element in differet way.
* Reason being is we do in traditional way, sum might get overflow.
* And at times, we might get OutOfBondException
*
*/
middle = first + ((last - first) / 2);
out=isBadVersion(middle);
if(out==false){
first=middle+1;
}
else if(out==true){
if(isBadVersion(middle-1)==false)
return middle;
else
last=middle-1;
}
}
return -1;
}
}