Given a sorted binary array arr[] consisting only of 0s and 1s, where all 1s appear before all 0s, find the number of zeroes in the array.
Examples:
Input: arr[] = {1, 1, 1, 1, 0, 0}
Output: 2
Explanation: There are 2 zeros in the array.Input: arr[] = {1, 0, 0, 0, 0}
Output: 4
Explanation: There are 4 zeros in the array.
Try It Yourself
Table of Content
[Naive Approach] Linear Search - O(n) Time and O(1) Space
The idea is to traverse the array from left to right and find the first occurrence of 0.
Since all 1s are placed before all 0s, once the first 0 is found at index i, all elements from i to n - 1 are 0. Therefore, the count of zeroes is n - i.
If no 0 is found, the answer is 0.
#include <iostream>
#include <vector>
using namespace std;
// Method to count total zeros using Linear Search
int countZeroes(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n; i++) {
// First occurrence of 0 found
if (arr[i] == 0) {
return n - i;
}
}
// No zeros present
return 0;
}
int main() {
vector<int> arr = {1, 1, 1, 1, 0, 0, 0};
cout << countZeroes(arr) << endl;
return 0;
}
#include <stdio.h>
// Method to count total zeros using Linear Search
int countZeroes(int arr[], int n) {
for (int i = 0; i < n; i++) {
// First occurrence of 0 found
if (arr[i] == 0) {
return n - i;
}
}
// No zeros present
return 0;
}
int main() {
int arr[] = {1, 1, 1, 1, 0, 0, 0};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", countZeroes(arr, n));
return 0;
}
class GFG {
// Method to count total zeros using Linear Search
public static int countZeroes(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
// First occurrence of 0 found
if (arr[i] == 0) {
return n - i;
}
}
// No zeros present
return 0;
}
public static void main(String[] args) {
int[] arr = {1, 1, 1, 1, 0, 0, 0};
System.out.println(countZeroes(arr));
}
}
# Method to count total zeros using Linear Search
def countZeroes(arr):
n = len(arr)
for i in range(n):
# First occurrence of 0 found
if arr[i] == 0:
return n - i
# No zeros present
return 0
if __name__ == "__main__":
arr = [1, 1, 1, 1, 0, 0, 0]
print(countZeroes(arr))
using System;
using System.Collections.Generic;
class GFG {
// Method to count total zeros using Linear Search
public static int countZeroes(List<int> arr) {
int n = arr.Count;
for (int i = 0; i < n; i++) {
// First occurrence of 0 found
if (arr[i] == 0) {
return n - i;
}
}
// No zeros present
return 0;
}
public static void Main() {
List<int> arr = new List<int> { 1, 1, 1, 1, 0, 0, 0 };
Console.WriteLine(countZeroes(arr));
}
}
// Method to count total zeros using Linear Search
function countZeroes(arr) {
let n = arr.length;
for (let i = 0; i < n; i++) {
// First occurrence of 0 found
if (arr[i] === 0) {
return n - i;
}
}
// No zeros present
return 0;
}
// Driver Code
let arr = [1, 1, 1, 1, 0, 0, 0];
console.log(countZeroes(arr));
Output
3
[Expected Approach] Binary Search - O(log n) Time and O(1) Space
Since the array is sorted, binary search can find the first occurrence of 0 in array.
- Initialize low = 0 and high = n - 1.
- Calculate the middle index mid.
- If arr[mid] == 0, search the left half by setting high = mid - 1.
- Otherwise, search the right half by setting low = mid + 1.
- When the search ends, low points to the first occurrence of 0, or n if no zero exists.
- Return n - low as the number of zeroes.
#include <iostream>
#include <vector>
using namespace std;
int countZeroes(const vector<int>& arr) {
int n = arr.size();
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// Search left for the first occurrence of 0
if (arr[mid] == 0) {
high = mid - 1;
} else {
// Search right
low = mid + 1;
}
}
// Count zeros from the first zero to the end
return n - low;
}
int main() {
vector<int> arr = {1, 1, 1, 1, 0, 0, 0};
cout << countZeroes(arr) << endl;
return 0;
}
class GFG {
static int countZeroes(int[] arr) {
int n = arr.length;
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// Search left for the first occurrence of 0
if (arr[mid] == 0) {
high = mid - 1;
} else {
// Search right
low = mid + 1;
}
}
// Count zeros from the first zero to the end
return n - low;
}
public static void main(String[] args) {
int[] arr = {1, 1, 1, 1, 0, 0, 0};
System.out.println(countZeroes(arr));
}
}
def countZeroes(arr):
n = len(arr)
low = 0
high = n - 1
while low <= high:
mid = low + (high - low) // 2
# Search left for the first occurrence of 0
if arr[mid] == 0:
high = mid - 1
else:
# Search right
low = mid + 1
# Count zeros from the first zero to the end
return n - low
if __name__ == "__main__":
arr = [1, 1, 1, 1, 0, 0, 0]
print(countZeroes(arr))
using System;
using System.Collections.Generic;
class GFG {
static int countZeroes(List<int> arr) {
int n = arr.Count;
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// Search left for the first occurrence of 0
if (arr[mid] == 0) {
high = mid - 1;
} else {
// Search right
low = mid + 1;
}
}
// Count zeros from the first zero to the end
return n - low;
}
static void Main() {
List<int> arr = new List<int> {1, 1, 1, 1, 0, 0, 0};
Console.WriteLine(countZeroes(arr));
}
}
function countZeroes(arr) {
let n = arr.length;
let low = 0, high = n - 1;
while (low <= high) {
let mid = low + Math.floor((high - low) / 2);
// Search left for the first occurrence of 0
if (arr[mid] === 0) {
high = mid - 1;
} else {
// Search right
low = mid + 1;
}
}
// Count zeros from the first zero to the end
return n - low;
}
// Driver code
let arr = [1, 1, 1, 1, 0, 0, 0];
console.log(countZeroes(arr));
Output
3