count number of zeros in array in o 28logn 29

Solutions on MaxInterview for count number of zeros in array in o 28logn 29 by the best coders in the world

showing results for - "count number of zeros in array in o 28logn 29"
Claudio
11 Feb 2019
1int firstZero(int arr[], int low, int high) 
2{
3	if (high >= low) 
4    { 
5        // Check if mid element is first 0 
6        int mid = low + (high - low)/2; 
7        if (( mid == 0 || arr[mid-1] == 1) && arr[mid] == 0) 
8            return mid; 
9        if (arr[mid] == 1)  // If mid element is not 0 
10            return firstZero(arr, (mid + 1), high); 
11        else  // If mid element is 0, but not first 0 
12            return firstZero(arr, low, (mid -1)); 
13    } 
14    return -1; 
15}   
16// A wrapper over recursive function firstZero() 
17int countZeroes(int arr[], int n) 
18{ 
19    // Find index of first zero in given array 
20    int first = firstZero(arr, 0, n-1); 
21    // If 0 is not present at all, return 0 
22    if (first == -1) 
23        return 0; 
24    return (n - first); 
25} 
26
27//Credits : GeeksForGeeks