> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/wan-quan-an-zhao-jiu-zhang-suan-fa-shua-de-60-dao-zuo-you/binary-search/278.-first-bad-version.md).

# 278. First Bad Version

\# Easy

{% hint style="success" %}
Find the first position of target using binary search
{% endhint %}

```java
// Some 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 firstBad = n;
        int start = 1, end = n;
        while(start <= end) {
            int mid = start + (end - start)/2;
            if(isBadVersion(mid)) {
                firstBad = mid;
                end = mid - 1;
            } else
                start = mid + 1;
        }
        return firstBad;
    }
}a
```
