# First Bad Version in Python

PythonServer Side ProgrammingProgramming

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 −

Live Demo

first_bad = 0
return True
return False
class Solution:
if n <2:
return n
start = 1
end = n
while(start<=end):
mid = (start+end)//2
return mid
end = mid-1
else:
start = mid+1
ob1 = Solution()
print(op)
5
4
4