First Bad Version problem results
First Blood
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 API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int left = 0;
int right = n-1;
while(left <= right)
{
int pivot = left + (right - left)/2;
if(isBadVersion(pivot + 1))
{
//check if is first bad version
if(isBadVersion(pivot))
{
right = pivot - 1;
}
else
{
return pivot + 1;
}
}
else
{
left = pivot + 1;
}
}
return -1;
}
};
}