Loading practice problems…
Loading your practice workspace…
Loading code editor…
14 / 29
Sign in
Algorithms/Binary Search

First Bad Version

easy20 min

Versions are numbered from 1 through n. Once a version is bad, every later version is also bad.

Given n and firstBad, return the first bad version. The firstBad parameter replaces LeetCode's external isBadVersion API in this exercise.

Example 1

Inputn = 5, firstBad = 4

Output4

Versions 1 through 3 are good. Versions 4 and 5 are bad, so 4 is the first bad version.

Example 2

Inputn = 1, firstBad = 1

Output1

Version 1 is the only version, and it is bad.

Constraints

  • 1 ≤ firstBad ≤ n ≤ 2³¹ - 1
  • Minimize the number of version checks.
Binary SearchBoundaries