Given a sorted binary array arr[] consisting only of 0s and 1s, where all the 1s are placed before all the 0s, find the count of 0s 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.
Table of Content
[Naive Approach] Using 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
We use Binary Search to find index of first 0. Let index of first 0 be i, all elements from index i to n - 1 are 0. Hence, the total number of zeroes is n - i.
To do Binary Search, we check the mid and handle the following cases.
- If we find a 1, we go to right half.
- If 0, we go to left half if there is 0 before this.
- If 0 and previous element is 1, we found first 0 index.
The Binary Search works as follows:
- Initialize low = 0 and high = n - 1.
- Find the middle index mid.
- If arr[mid] == 0, then mid can be the first occurrence of 0. So, search for an earlier 0 by setting high = mid - 1.
- If arr[mid] == 1, then the first 0 must be on the right side. So, set low = mid + 1.
- After the loop, low points to the first occurrence of 0.
- The number of zeroes is n - low.
- If the array contains no zeroes, low becomes n, so the answer is 0.
#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;
// Check if mid is the first 0
if (arr[mid] == 0) {
if (mid == 0 || arr[mid - 1] == 1) {
return n - mid;
}
// Search left for first occurrence
high = mid - 1;
} else {
// Search right
low = mid + 1;
}
}
// No zeros present
return 0;
}
int main() {
vector<int> arr = {1, 1, 1, 1, 0, 0, 0};
cout << countZeroes(arr) << endl;
return 0;
}
class GFG {
public static int countZeroes(int[] arr)
{
int n = arr.length;
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// Check if mid is the first 0
if (arr[mid] == 0) {
if (mid == 0 || arr[mid - 1] == 1) {
return n - mid;
}
// Search left for first occurrence
high = mid - 1;
}
else {
// Search right
low = mid + 1;
}
}
// 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));
}
}
def countZeroes(arr):
n = len(arr)
low, high = 0, n - 1
while low <= high:
mid = low + (high - low) // 2
# Check if mid is the first 0
if arr[mid] == 0:
if mid == 0 or arr[mid - 1] == 1:
return n - mid
# Search left for first occurrence
high = mid - 1
else:
# Search right
low = mid + 1
# 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
{
public 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;
// Check if mid is the first 0
if (arr[mid] == 0)
{
if (mid == 0 || arr[mid - 1] == 1)
{
return n - mid;
}
// Search left for first occurrence
high = mid - 1;
}
else
{
// Search right
low = mid + 1;
}
}
// 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));
}
}
function countZeroes(arr) {
let n = arr.length;
let low = 0, high = n - 1;
while (low <= high) {
let mid = Math.floor(low + (high - low) / 2);
// Check if mid is the first 0
if (arr[mid] === 0) {
if (mid === 0 || arr[mid - 1] === 1) {
return n - mid;
}
// Search left for first occurrence
high = mid - 1;
} else {
// Search right
low = mid + 1;
}
}
// No zeros present
return 0;
}
// Driver Code
let arr = [1, 1, 1, 1, 0, 0, 0];
console.log(countZeroes(arr));
Output
3