dsa · easy

First Bad Version

A product has versions 1 .. n. Once a version goes bad, every later version is also bad. nums has length n; nums[i] is 1 if version i + 1 is bad, and 0 if it is still good. The flags are monotonic: zeros, then ones.

Arguments

Return the 1-based index of the first bad version. If every flag is 0, return n + 1.

Example

n = 5, nums = [0,0,1,1,1]

Version 1 is good, version 2 is good, version 3 is bad — and 4 and 5 stay bad. The first bad version is 3.

n = 1, nums = [1]1 (the only version is already bad).

Constraints

1 <= n <= 10^5 n == nums.length nums[i] is 0 or 1 nums is sorted in non-decreasing order Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
5
[0,0,1,1,1]

Expected:
3

Example 2

Input:
1
[1]

Expected:
1

Example 3

Input:
1
[0]

Expected:
2

Open in the Dojo editor