30 Days of Code [Day 23]
QUESTION : 278. First Bad Version
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define fo(i,s,n) for(int i=s;i<n;++i)
#define of(i,s,n) for(int i=s-1;i>=n;--i)
#define fv(V) for( auto &it : V )
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
class Solution {
public:
int firstBadVersion(int n) {
int l = 0;
int r = n;
int res = 0;
while(l <= r){
int mid = l + (r-l)/2;
if(!isBadVersion(mid)){
l = mid + 1;
}
else{
res = mid;
r = mid-1;
}
}
return res;
}
};