First Bad Version

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 n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example:
Given n = 5, and version = 4 is the first bad version.

call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true

Then 4 is the first bad version.  
 
METHOD 1:
 

 
/* 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;
    int right = n;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (isBadVersion(mid)) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
    }
} 



Try it on Leetcode

Here, we have to find smallest bad version.To optimize search we are using binary search.

1) Initialize left as 1 and right as n.

2) We have to find mid. If we use mid = left+right/2 you will face overflow issue. To over come this,
calculate mid using mid = left+(right - left)/2 [To understand this, read below example]
     Example: Consider int can hold a max of 200 so take left=80 right=160 , left+right=240> than int can hold i.e overflow but 80+(160-80)/2 = 80+40, the number never went greater than 200.

3)Do iteration till left is smaller than right. Though mid is badVersion, we cannot confirm it as small bad version. So, we update that mid as right and incrementing left until it fails loop.
Finally, left will be the smallest badVersion.

Time Complexity: O(log n)

NOTE:[FOR LEARNING]

Below solution is easiest way to find smallest badVersion i.e., using brute force method. But it will result in TLE(Time Limit Exceeded). Have a look over this solution


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

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
   
        for(int i=1;i<=n;i++){
            if(isBadVersion(i)){
                return i;
            }
        }
        return n;
   
    }
}

Time Complexity: O(n)

Here, though it is O(n) it is not acceptable in this submission.


Click here for May month challenges with solution and explanation
Click here for April month challenges with solution and explanation


Please let us know your views in comment section

Comments

Popular posts from this blog

First Unique Character in a String

Balanced Binary Tree

Majority Element

Sequential Digits