Count the Zeros | Practice | GeeksforGeeks

Given an array of size N consisting of only 0's and 1's. The array is sorted in such a manner that all the 1's are placed first and then they are followed by all the 0's. Find the count of all the 0's.
Example 1:
Input:
N = 12
Arr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}
Output: 3
Explanation: There are 3 0's in the given array.


Example 2:
Input:
N = 5
Arr[] = {0, 0, 0, 0, 0}
Output: 5
Explanation: There are 5 0's in the array.

Your Task:
You don't need to read input or print anything. Your task is to complete the function countZeroes() which takes the array of integers arr[] and its size n as input parameters and returns the number of zeroes present in the array.

Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 105
0 <= Arr[i] <= 1
 
0
zarvis17 hours ago
int countZeroes(int arr[], int n) {
    // code here
    int count=0;
    for(int i=0;i<n; i++)
    {
        if(arr[i]==0)
          count++;
    }
    return count;
}
0
baibhavrajputt1 day ago
Java Binary Search Solution
class Solution {
    int countZeroes(int[] arr, int n) {
        // code here
        int left = 0;
        int right = n-1;
        
        while(left <= right){
            int mid = left + (right - left)/2;
            
            if(arr[mid] == 0){
                if(mid == 0 || arr[mid-1] != arr[mid] )
                    return n-mid;
                else 
                    right = mid - 1;
            }
            else 
                left = mid + 1;
        }
        return 0;
    }

}
0
nikolaoskor82 days ago
int countZeroes(int arr[], int n) {   int zeroes=0, i=n-1;   while(arr[i]==0 && i>=0) {       zeroes++;       i--;   }   return zeroes;} 
0
kushganesh122 days ago
int countZeroes(int[] a, int n) {      int count=0;      int l=0;int p=a.length-1;      while(l<=p)      {          if(a[l]==0)          count++;           if(a[p]==0)          count++;          if(l==p&&a[l]==a[p])                   if(a[l]==0)                   count--;          l++;p--;                }      return count
0
harshscode6 days ago
int z=0;      for(int i=0;i<n;i++)   {       if(a[i]==0)       z=z+1;   }   return z;   
0
vishutyagi71 week ago
c++ easy solution with 0.1 sec 
if(arr[0]!=0)   {       int l=0,m,h=n-1;       m=(l+h)/2;       do       {           if(arr[m]==1)           {               l=m+1;           }           else if(arr[m]==0)           {               h=m;           }           m=(l+h)/2;       }while(l<h);       return n-l;   }   else   {       return n;   }
0
animeshmishra971 week ago
C++ Solution
int countZeroes(int arr[], int n) {   int a[100000]={0};   for(int i=0;i<n;i++){       a[arr[i]]++;   }   return a[0];}
0
shrustis1763 weeks ago
C++ solution using Binary Search
 
int countZeroes(int arr[], int n) {   // code here   int l=0, h=n-1, mid, cnt;      if(arr[0] == 0)       return n;          while(l<=h)   {       mid = l+(h-l)/2;              if(arr[mid] ==1)           l=mid+1;       else           h=mid-1;   }      cnt=n-l;      return cnt;}
-1
darhain1014 weeks ago
int countZeroes(int arr[], int n) {       // code here       int count=0;       for(int i=0;i<n;i++)       {           if(arr[i]==0)           {               count++;           }       }       return count;          }
0
codewithshoaib191 month ago
Time 0.5 Accuracy 100%
 int count=0;       int s=0;       int e=n-1;       if (arr[0] == 0) {           return arr.length;       }       if (arr[e] == 1) {           return 0;       }       while(s<=e){           int mid=s+(e-s)/2;           if(arr[mid]==1 && arr[mid+1]==0){               return (n)- (mid+1);           }           else if(arr[mid]==0 && arr[mid-1]==1){               return n-(mid);           }           if(arr[mid]==0 && arr[mid-1]==0){               e=mid-1;           }           else {               s=mid+1;           }                  }       return count;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.