First Bad Version

Find the first index where isBadVersion(i) becomes true — boundary binary search.

Easy
Time O(log n) Space O(1)
LeetCode
3 min read
binary-search

Problem#

Versions 1..n; once a version is bad, all later ones are too. Given isBadVersion(version), find the first bad one.

Examples#

  • n = 5, bad = 44
  • n = 1, bad = 11

Constraints#

  • 1 <= bad <= n <= 2^31 - 1.

Approach#

Boundary binary search. lo, hi initially 1, n. While lo < hi, mid = lo + (hi - lo) / 2; if isBadVersion(mid), the answer is at most mid (hi = mid); else (lo = mid + 1).

Solution#

First Bad Version
class Solution {
public:
int firstBadVersion(int n) {
int lo = 1, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (isBadVersion(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}
};

Editorial#

The lo < hi template is for finding the first index satisfying a monotone predicate. hi = mid (not mid - 1) keeps the candidate in the search range; lo = mid + 1 eliminates a confirmed non-answer. Loop invariant: answer ∈ [lo, hi].

Complexity#

  • Time: O(log n).
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.