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

You are given an API

**Example:**

#### Solution

Here we can apply Binary Search between

First calculate the mid index and check for it, If mid index number version is bad version then we need to search on left side, else need to search on right side of mid.

###### Code

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/* 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 left=1, right=n; while (left <= right) { int mid = left+((right - left) / 2); if(!isBadVersion(mid)){ left=mid+1; }else{ right=mid-1; } } return left; } } |

We encourage you to write a comment if you have a better solution or having any doubt on the above topic.