-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLc_278.java
More file actions
46 lines (38 loc) · 1.07 KB
/
Lc_278.java
File metadata and controls
46 lines (38 loc) · 1.07 KB
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
36
37
38
39
40
41
42
43
44
45
46
//Lc-278. First Bad Version
/*
* First clue to solve this question is the versions are
in increasing order (1,2,3,4,...,n) which is monotonic
* So, we can apply binary search
* As per the question if say 4 is the first bad number
so later versions after 4 will be bad.
* So, we just have to find a bad number; and apply Binary Search
* */
public class Lc_278 {
public static void main(String[] args) {
System.out.println(firstBadVersion(100));
}
// api to check for bad version
static boolean isBadVersion(int n){
int unknownFirstBadVersion = 10; // this is the unknown first bad version
if(n < unknownFirstBadVersion){
return false;
}
else{
return true;
}
}
// check for first bad version
static int firstBadVersion(int n) {
int l = 1, r = n;
while(l < r){
int mid = l + (r - l)/2;
if(isBadVersion(mid)){
r = mid;
}
else{
l = mid + 1;
}
}
return r;
}
}