## Algorithm

Problem Name: 278. First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have `n` versions `[1, 2, ..., n]` and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API `bool isBadVersion(version)` which returns whether `version` is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example 1:

```Input: n = 5, bad = 4
Output: 4
Explanation:
Then 4 is the first bad version.
```

Example 2:

```Input: n = 1, bad = 1
Output: 1
```

Constraints:

• `1 <= bad <= n <= 231 - 1`

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
// Forward declaration of isBadVersion API.
​
int left, right, mid;
left = 1; right = n;
while (left  <  right) {
mid = left + (right - left) / 2;
right = mid;
} else {
left = mid + 1;
}
}
return right;
}
``````
Copy The Code &

Input

cmd
n = 5, bad = 4

Output

cmd
4

### #2 Code Example with C++ Programming

```Code - C++ Programming```

``````

class Solution {
public:
int lower = 1, upper = n;
while(lower  <  upper)
if(isBadVersion(lower + (upper - lower)/2)) upper = lower + (upper - lower)/2;
else lower = lower + (upper - lower)/2 + 1;
return lower;
}
};
``````
Copy The Code &

Input

cmd
n = 5, bad = 4

Output

cmd
4

### #3 Code Example with Java Programming

```Code - Java Programming```

``````
public class Solution extends VersionControl {
if (n == 1) {
return 1;
}
int start = 1;
int end = n;
int minIdx = Integer.MAX_VALUE;
while (start  < = end) {
int mid = start + (end - start) / 2;
minIdx = Math.min(minIdx, mid);
end = mid - 1;
}
else {
start = mid + 1;
}
}
return minIdx;
}
}
``````
Copy The Code &

Input

cmd
n = 1, bad = 1

Output

cmd
1

### #4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````

return function(n) {
let left = 1;
let right = n;
while (left  <  right) {
let mid = left + Math.floor( (right - left) / 2 );
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
};
``````
Copy The Code &

Input

cmd
n = 1, bad = 1

Output

cmd
1

### #5 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
"""
:type n: int
:rtype: int
"""
l, r = 0, n
while l <= r:
mid = (l + r) // 2
r = mid - 1
else:
l = mid + 1
return r + 1
``````
Copy The Code &

Input

cmd
n = 5, bad = 4

Output

cmd
4

### #6 Code Example with C# Programming

```Code - C# Programming```

``````
namespace LeetCode
{

{
{
int left = 1;
int right = n;
while (left  <  right)
{
int mid = left + (right - left) / 2;
right = mid;
else
left = mid + 1;
}
return left;
}
}

public class VersionControl
{
{
return version > 3;
}
}
}
``````
Copy The Code &

Input

cmd
n = 5, bad = 4

Output

cmd
4