Find First Index Where Zeros Begin in Binary Array
EasyUpdated: Sep 13, 2025
Problem
Given a binary array (only 1’s followed by 0’s), find the first index where 0 starts. If no 0 is present, report accordingly. If the array contains only 0’s, return the first index (0).
Examples
Example 1
Input: nums = [1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0]
Output: 10
Explanation: The first zero appears at index 10.
Example 2
Input: nums = [1,1,1]
Output: -1
Explanation: No zero is present in the array.
Example 3
Input: [0,0,0]
Output: 0
Explanation: All elements are zero, so the first index is 0.
Solution
Method 1 – Linear Scan
Intuition
Scan the array from left to right. The first time you see a 0 in nums, that’s the answer. If no 0 is found, report accordingly.
Approach
- Start from index
0and iterate through the arraynums. - If you find a
0, return its index immediately. - If you reach the end without finding a
0, report that the array does not have a0.
Code
C++
class Solution {
public:
int findFirstZero(const std::vector<int>& arr) {
for (int i = 0; i < arr.size(); ++i) {
if (arr[i] == 0) return i;
}
return -1; // No zero present
}
};
Java
class Solution {
public int findFirstZero(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) return i;
}
return -1; // No zero present
}
}
Python
class Solution:
def find_first_zero(self, arr: list[int]) -> int:
for i, val in enumerate(arr):
if val == 0:
return i
return -1 # No zero present
Complexity
- ⏰ Time complexity:
O(n)— Must check each element in the worst case. - 🧺 Space complexity:
O(1)— Only a few variables for iteration.
Method 2 – Binary Search
Intuition
Since the array nums is sorted (all 1s then all 0s), we can use binary search to find the transition point efficiently.
Approach
- Set
leftandrightpointers to the start and end of the arraynums. - While
left≤right:
- Find
midindex. - If
nums[mid] == 0and (mid == 0ornums[mid-1] == 1), returnmid. - If
nums[mid] == 1, search right half (left = mid + 1). - If
nums[mid] == 0andnums[mid-1] == 0, search left half (right = mid - 1).
- If no
0is found, return-1.
Code
C++
class Solution {
public:
int findFirstZero(const std::vector<int>& arr) {
int left = 0, right = arr.size() - 1, ans = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == 0) {
if (mid == 0 || arr[mid - 1] == 1) return mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
};
Java
class Solution {
public int findFirstZero(int[] arr) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == 0) {
if (mid == 0 || arr[mid - 1] == 1) return mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
}
Python
class Solution:
def find_first_zero(self, arr: list[int]) -> int:
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == 0:
if mid == 0 or arr[mid - 1] == 1:
return mid
right = mid - 1
else:
left = mid + 1
return -1
Complexity
- ⏰ Time complexity:
O(log n)— Each step halves the search space. - 🧺 Space complexity:
O(1)— Only a few pointers and variables.