count number of zeros in an array python code example
Example 1: count number of zeros in array in O(logN)
int firstZero(int arr[], int low, int high)
{
if (high >= low)
{
int mid = low + (high - low)/2;
if (( mid == 0 || arr[mid-1] == 1) && arr[mid] == 0)
return mid;
if (arr[mid] == 1)
return firstZero(arr, (mid + 1), high);
else
return firstZero(arr, low, (mid -1));
}
return -1;
}
int countZeroes(int arr[], int n)
{
int first = firstZero(arr, 0, n-1);
if (first == -1)
return 0;
return (n - first);
}
Example 2: count number of zeros in a number python
# credit to Stack Overflow user in source link
>>> def count_zeros(number):
... return str(number).count('0')
...
>>> count_zeros(49690101904335902069)
5