> For the complete documentation index, see [llms.txt](https://hao-fu-1.gitbook.io/oj/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hao-fu-1.gitbook.io/oj/binarysearch/first_bad_version.md).

# First Bad Version

First Bad Version

<https://leetcode.com/problems/first-bad-version/>

## Tips

见本章首页

## Code

```
/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        int start = 0, end = n;
        while (start < end) {
            int mid = start + (end - start) / 2;
            if (!isBadVersion(mid)) {
                start = mid + 1; 
            } else {
                end = mid;
            }
        }

        return start;
    }
}
```

## Analysis

O(lgn)
