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

Example 2

Inputn = 1, firstBad = 1

Output1

Constraints

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