Problem · Binary Search
First Bad Version
Learn this problemProblem statement
A product has versions numbered from 1 through n. Once a version is bad, every later version is also bad.
You are given a boolean array isBad of length n, where isBad[i] reports whether version i + 1 is bad. At least one version is bad.
Return the number of the first bad version.
Function
firstBadVersion(isBad: boolean[]) → intExamples
Example 1
isBad = [false,false,false,true,true]return = 4Versions 1 through 3 are good, and version 4 is the first bad version.
Example 2
isBad = [true]return = 1The only version is bad, so it is also the first bad version.
Example 3
isBad = [false,true,true,true]return = 2Version 1 is good, while version 2 and every later version are bad.