461. Count Zeros in Sorted Binary Array

EasyBinary SearchArrayBinary Search

Given a sorted binary array nums (containing only 0s and 1s, all 0s before all 1s), return the count of 0s in the array. Solve in O(log n).

Input: A sorted binary array nums (0s followed by 1s).

Output: Count of 0s.

Examples

Example 1
Input: [0,0,0,1,1,1,1]
Output: 3
Explanation: First 1 is at index 3. Count of 0s = 3.
Example 2
Input: [0,0,0,0,0]
Output: 5
Explanation: All zeros. Count = 5.
Example 3
Input: [1,1,1,1]
Output: 0
Explanation: No zeros. Count = 0.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →