Open In App

Find the number of zeroes

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of 1s and 0s which has all 1s first followed by all 0s. Find the number of 0s. Count the number of zeroes in the given array.
Examples : 

Input: arr[] = {1, 1, 1, 1, 0, 0}
Output: 2

Input: arr[] = {1, 0, 0, 0, 0}
Output: 4

Input: arr[] = {0, 0, 0}
Output: 3

Input: arr[] = {1, 1, 1, 1}
Output: 0
Recommended Practice

Approach 1: A simple solution is to traverse the input array. As soon as we find a 0, we return n – index of first 0. Here n is number of elements in input array. Time complexity of this solution would be O(n).

Implementation of above approach is below:

C++




// A program to find the number of zeros
#include <bits/stdc++.h>
using namespace std;
int firstzeroindex(int arr[], int n)
{
    for (int i = 0; i < n; i++) {
        if (arr[i] == 0) {
            return i;
        }
    }
    return -1;
}
int main()
{
    int arr[] = { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = firstzeroindex(arr, n);
    if (x == -1) {
        cout << "Count of zero is 0" << endl;
    }
    else {
        cout << "count of zero is " << n - x << endl;
    }
    return 0;
}
// this code is contributed by machhaliya muhammad


Java




// A program to find the number of zeros
 
import java.io.*;
 
class GFG {
    static int firstzeroindex(int arr[], int n)
    {
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0) {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args)
    {
        int arr[] = { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
        int n = arr.length;
        int x = firstzeroindex(arr, n);
        if (x == -1) {
            System.out.println("Count of zero is 0");
        }
        else {
            System.out.print("count of zero is ");
              System.out.println(n-x);
        }
    }
}
 
// This code is contributed by Abhijeet Kumar(abhijeet19403)


Python3




class GFG :
    @staticmethod
    def  firstzeroindex( arr,  n) :
        i = 0
        while (i < n) :
            if (arr[i] == 0) :
                return i
            i += 1
        return -1
    @staticmethod
    def main( args) :
        arr = [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
        n = len(arr)
        x = GFG.firstzeroindex(arr, n)
        if (x == -1) :
            print("Count of zero is 0")
        else :
            print("count of zero is ", end ="")
            print(n - x)
     
 
if __name__=="__main__":
    GFG.main([])
     
    # This code is contributed by aadityaburujwale.


C#




// A program to find the number of zeros
using System;
 
public class GFG{
 
          static int firstzeroindex(int[] arr, int n)
    {
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0) {
                return i;
            }
        }
        return -1;
    }
    public static void Main()
    {
        int[] arr = { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
        int n = arr.Length;
        int x = firstzeroindex(arr, n);
        if (x == -1) {
              Console.WriteLine("Count of zero is 0");
        }
        else {
            Console.Write("count of zero is ");
              Console.WriteLine(n-x);
        }
    }
 
}
 
// This code is contributed by Abhijeet Kumar(abhijeet19403)


Javascript




<script>
 
// A program to find the number of zeros
 
function firstzeroindex(arr, n)
    {
        for (let i = 0; i < n; i++) {
            if (arr[i] == 0) {
                return i;
            }
        }
        return -1;
    }
     
 
        let arr = [ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 ];
        let n = arr.length;
        let x = firstzeroindex(arr, n);
        if (x == -1) {
            document.write("Count of zero is 0");
        }
        else {
            document.write("count of zero is " + (n-x));
  
        }
 
</script>


Output

count of zero is 6

Time complexity: O(n) where n is size of arr.

Space Complexity: O(1) as we are not using any extra space.

Approach 2: Since the input array is sorted, we can use Binary Search to find the first occurrence of 0. Once we have index of first element, we can return count as n – index of first zero.

Implementation:

C




// A divide and conquer solution to find count of zeroes in an array
// where all 1s are present before all 0s
#include <stdio.h>
 
/* if 0 is present in arr[] then returns the index of FIRST occurrence
of 0 in arr[low..high], otherwise returns -1 */
int firstZero(int arr[], int low, int high)
{
    if (high >= low)
    {
        // Check if mid element is first 0
        int mid = low + (high - low)/2;
        if (( mid == 0 || arr[mid-1] == 1) && arr[mid] == 0)
            return mid;
 
        if (arr[mid] == 1) // If mid element is not 0
            return firstZero(arr, (mid + 1), high);
        else // If mid element is 0, but not first 0
            return firstZero(arr, low, (mid -1));
    }
    return -1;
}
 
// A wrapper over recursive function firstZero()
int countZeroes(int arr[], int n)
{
    // Find index of first zero in given array
    int first = firstZero(arr, 0, n-1);
 
    // If 0 is not present at all, return 0
    if (first == -1)
        return 0;
 
    return (n - first);
}
 
/* Driver program to check above functions */
int main()
{
    int arr[] = {1, 1, 1, 0, 0, 0, 0, 0};
    int n = sizeof(arr)/sizeof(arr[0]);
    printf("Count of zeroes is %d", countZeroes(arr, n));
    return 0;
}


C++




// A divide and conquer solution to
// find count of zeroes in an array
// where all 1s are present before all 0s
#include <bits/stdc++.h>
using namespace std;
 
/* if 0 is present in arr[] then
returns the index of FIRST occurrence
of 0 in arr[low..high], otherwise returns -1 */
int firstZero(int arr[], int low, int high)
{
    if (high >= low)
    {
        // Check if mid element is first 0
        int mid = low + (high - low) / 2;
        if ((mid == 0 || arr[mid - 1] == 1) &&
                         arr[mid] == 0)
            return mid;
 
        // If mid element is not 0
        if (arr[mid] == 1)
            return firstZero(arr, (mid + 1), high);
         
        // If mid element is 0, but not first 0   
        else
            return firstZero(arr, low, (mid -1));
    }
    return -1;
}
 
// A wrapper over recursive function firstZero()
int countZeroes(int arr[], int n)
{
    // Find index of first zero in given array
    int first = firstZero(arr, 0, n - 1);
 
    // If 0 is not present at all, return 0
    if (first == -1)
        return 0;
 
    return (n - first);
}
 
// Driver Code
int main()
{
    int arr[] = {1, 1, 1, 0, 0, 0, 0, 0};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Count of zeroes is "
         << countZeroes(arr, n);
    return 0;
}
 
// This code is contributed by SoumikMondal


Java




// A divide and conquer solution to find count of zeroes in an array
// where all 1s are present before all 0s
 
class CountZeros
{
    /* if 0 is present in arr[] then returns the index of FIRST occurrence
       of 0 in arr[low..high], otherwise returns -1 */
    int firstZero(int arr[], int low, int high)
    {
        if (high >= low)
        {
            // Check if mid element is first 0
            int mid = low + (high - low) / 2;
            if ((mid == 0 || arr[mid - 1] == 1) && arr[mid] == 0)
                return mid;
 
            if (arr[mid] == 1) // If mid element is not 0
                return firstZero(arr, (mid + 1), high);
            else // If mid element is 0, but not first 0
                return firstZero(arr, low, (mid - 1));
        }
        return -1;
    }
 
    // A wrapper over recursive function firstZero()
    int countZeroes(int arr[], int n)
    {
        // Find index of first zero in given array
        int first = firstZero(arr, 0, n - 1);
 
        // If 0 is not present at all, return 0
        if (first == -1)
            return 0;
 
        return (n - first);
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        CountZeros count = new CountZeros();
        int arr[] = {1, 1, 1, 0, 0, 0, 0, 0};
        int n = arr.length;
        System.out.println("Count of zeroes is " + count.countZeroes(arr, n));
    }
}


Python3




# A divide and conquer solution to
# find count of zeroes in an array
# where all 1s are present before all 0s
 
# if 0 is present in arr[] then returns
# the index of FIRST occurrence of 0 in
# arr[low..high], otherwise returns -1
def firstZero(arr, low, high):
 
    if (high >= low):
     
        # Check if mid element is first 0
        mid = low + int((high - low) / 2)
        if (( mid == 0 or arr[mid-1] == 1)
                      and arr[mid] == 0):
            return mid
         
        # If mid element is not 0
        if (arr[mid] == 1):
            return firstZero(arr, (mid + 1), high)
             
        # If mid element is 0, but not first 0
        else:
            return firstZero(arr, low, (mid - 1))
     
    return -1
 
# A wrapper over recursive
# function firstZero()
def countZeroes(arr, n):
 
    # Find index of first zero in given array
    first = firstZero(arr, 0, n - 1)
 
    # If 0 is not present at all, return 0
    if (first == -1):
        return 0
 
    return (n - first)
 
# Driver Code
arr = [1, 1, 1, 0, 0, 0, 0, 0]
n = len(arr)
print("Count of zeroes is",
        countZeroes(arr, n))
 
# This code is contributed by Smitha Dinesh Semwal


C#




// A divide and conquer solution to find
// count of zeroes in an array where all
// 1s are present before all 0s
using System;
 
class CountZeros
{
    /* if 0 is present in arr[] then returns
       the index of FIRST occurrence of 0 in
       arr[low..high], otherwise returns -1 */
    int firstZero(int []arr, int low, int high)
    {
        if (high >= low)
        {
            // Check if mid element is first 0
            int mid = low + (high - low) / 2;
            if ((mid == 0 || arr[mid - 1] == 1) &&
                                 arr[mid] == 0)
                return mid;
 
            if (arr[mid] == 1) // If mid element is not 0
                return firstZero(arr, (mid + 1), high);
                 
            else // If mid element is 0, but not first 0
                return firstZero(arr, low, (mid - 1));
        }
        return -1;
    }
 
    // A wrapper over recursive function firstZero()
    int countZeroes(int []arr, int n)
    {
        // Find index of first zero in given array
        int first = firstZero(arr, 0, n - 1);
 
        // If 0 is not present at all, return 0
        if (first == -1)
            return 0;
 
        return (n - first);
    }
 
    // Driver program to test above functions
    public static void Main()
    {
        CountZeros count = new CountZeros();
        int []arr = {1, 1, 1, 0, 0, 0, 0, 0};
        int n = arr.Length;
        Console.Write("Count of zeroes is " +
                       count.countZeroes(arr, n));
    }
}
 
// This code is contributed by nitin mittal.


PHP




<?php
// A divide and conquer solution to
// find count of zeroes in an array
// where all 1s are present before all 0s
 
/* if 0 is present in arr[] then
   returns the index of FIRST
   occurrence of 0 in arr[low..high],
   otherwise returns -1 */
function firstZero($arr, $low, $high)
{
    if ($high >= $low)
    {
         
        // Check if mid element is first 0
        $mid = $low + floor(($high - $low)/2);
         
        if (( $mid == 0 || $arr[$mid-1] == 1) &&
                                 $arr[$mid] == 0)
            return $mid;
 
        // If mid element is not 0
        if ($arr[$mid] == 1)
            return firstZero($arr, ($mid + 1), $high);
         
        // If mid element is 0,
        // but not first 0   
        else
            return firstZero($arr, $low,
                            ($mid - 1));
    }
    return -1;
}
 
// A wrapper over recursive
// function firstZero()
function countZeroes($arr, $n)
{
     
    // Find index of first
    // zero in given array
    $first = firstZero($arr, 0, $n - 1);
 
    // If 0 is not present
    // at all, return 0
    if ($first == -1)
        return 0;
 
    return ($n - $first);
}
     
    // Driver Code
    $arr = array(1, 1, 1, 0, 0, 0, 0, 0);
    $n = sizeof($arr);
    echo("Count of zeroes is ");
    echo(countZeroes($arr, $n));
     
// This code is contributed by nitin mittal
?>


Javascript




<script>
// A divide and conquer solution to find count of zeroes in an array
// where all 1s are present before all 0s
 
    /*
     * if 0 is present in arr then returns the index of FIRST occurrence of 0 in
     * arr[low..high], otherwise returns -1
     */
    function firstZero(arr , low , high) {
        if (high >= low) {
         
            // Check if mid element is first 0
            var mid = low + parseInt((high - low) / 2);
            if ((mid == 0 || arr[mid - 1] == 1) && arr[mid] == 0)
                return mid;
 
            if (arr[mid] == 1) // If mid element is not 0
                return firstZero(arr, (mid + 1), high);
            else // If mid element is 0, but not first 0
                return firstZero(arr, low, (mid - 1));
        }
        return -1;
    }
 
    // A wrapper over recursive function firstZero()
    function countZeroes(arr , n)
    {
     
        // Find index of first zero in given array
        var first = firstZero(arr, 0, n - 1);
 
        // If 0 is not present at all, return 0
        if (first == -1)
            return 0;
 
        return (n - first);
    }
 
    // Driver program to test above functions
 
        var arr = [ 1, 1, 1, 0, 0, 0, 0, 0 ];
        var n = arr.length;
        document.write("Count of zeroes is " + countZeroes(arr, n));
 
// This code is contributed by gauravrajput1
</script>


Output

Count of zeroes is 5

Time Complexity: O(Logn) where n is number of elements in arr[].

Auxiliary Space: O(logn)



Last Updated : 20 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads