Algorithm


Problem Name: 278. 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 returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

 

Example 1:

Input: n = 5, bad = 4
Output: 4
Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.

Example 2:

Input: n = 1, bad = 1
Output: 1

 

Constraints:

  • 1 <= bad <= n <= 231 - 1

Code Examples

#1 Code Example with C Programming

Code - C Programming


// Forward declaration of isBadVersion API.
bool isBadVersion(int version);
​
int firstBadVersion(int n) {
    int left, right, mid;
    left = 1; right = n;
    while (left  <  right) {
        mid = left + (right - left) / 2;
        if (isBadVersion(mid)) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return right;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 5, bad = 4

Output

x
+
cmd
4

#2 Code Example with C++ Programming

Code - C++ Programming


bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {
       int lower = 1, upper = n;
       while(lower  <  upper)
           if(isBadVersion(lower + (upper - lower)/2)) upper = lower + (upper - lower)/2;
           else lower = lower + (upper - lower)/2 + 1;
       return lower;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 5, bad = 4

Output

x
+
cmd
4

#3 Code Example with Java Programming

Code - Java Programming


public class Solution extends VersionControl {
  public int firstBadVersion(int n) {
    if (n == 1) {
      return 1;
    }
    int start = 1;
    int end = n;
    int minIdx = Integer.MAX_VALUE;
    while (start  < = end) {
      int mid = start + (end - start) / 2;
      if (isBadVersion(mid)) {
        minIdx = Math.min(minIdx, mid);
        end = mid - 1;
      }
      else {
        start = mid + 1;
      }
    }
    return minIdx;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 1, bad = 1

Output

x
+
cmd
1

#4 Code Example with Javascript Programming

Code - Javascript Programming


const solution = function(isBadVersion) {
    
    return function(n) {
      let left = 1;
      let right = n;
      while (left  <  right) {
        let mid = left + Math.floor( (right - left) / 2 );
        if (isBadVersion(mid)) {
            right = mid;
        } else {
            left = mid + 1;
        }
      }
      return left;
    };
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 1, bad = 1

Output

x
+
cmd
1

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        l, r = 0, n
        while l <= r:
            mid = (l + r) // 2
            if isBadVersion(mid):
                r = mid - 1
            else:
                l = mid + 1
        return r + 1
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 5, bad = 4

Output

x
+
cmd
4

#6 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{

    public class _0278_FirstBadVersion : 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;
        }
    }

    public class VersionControl
    {
        public bool IsBadVersion(int version)
        {
            return version > 3;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 5, bad = 4

Output

x
+
cmd
4
Advertisements

Demonstration


Previous
#275 Leetcode H-Index II Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#279 Leetcode Perfect Squares Solution in C, C++, Java, JavaScript, Python, C# Leetcode