# First Bad Version in Python

Suppose in a company, one product manager is leading a team who develops a new product. Suppose latest version fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version will be bad. So we have an array A with n elements [1, 2, … n] and we have to find the first bad version from this array.

Consider we have a function isBadVersion(version_id), this will return whether the version is bad or not. For an example, suppose n = 5, and version = 4 is first bad version. So if the isBadVersion(3) returns false isBadVersion(5) returns true, and isBadVersion(4) also returns true, then the first bad version is 4

To solve this, we will follow these steps −

• When n < 2, then return n
• Perform binary search approach to detect bad version using the given function.

## Example

Let us see the following implementation to get better understanding −

# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
class Solution:
"""
:type n: int
:rtype: int
"""
if n <2:
return n
start = 1
end = n
while(start<=end):
mid = (start+end)//2
return mid
start = mid+1
54
4