count number of zeros in array in O(logN)
C++
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);
}
//Credits : GeeksForGeeks
Also in C++:
- Title
- pbds in c++
- Category
- C++
- Title
- for c++
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- array syntax in c++
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- what is iterator in c++?
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- Find a element in a map C++
- Category
- C++
- Title
- ceil c++;
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- primos menores que
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- recursion in cpp with reference
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- switch statement c++
- Category
- C++
- Title
- how to read a comma delimited file into an array c++
- Category
- C++
- Title
- how to find the number of cycles in a graph C++
- Category
- C++
- Title
- apple and orange hackerrank solution in c++
- Category
- C++
- Title
- How to traverse in a tree iterative C++
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- pointer related problems dangling/wild pointers c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- ue4 c++ overlapping functions cpp setup
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- c++ class template
- Category
- C++
- Title
- c++ char if
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- clear console c++
- Category
- C++
- Title
- How to check if a triangular cycle exists in a graph
- Category
- C++
- Title
- flake8 max line length
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- fast io c++
- Category
- C++
- Title
- printf c++
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- pair c++
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- primeros numeors primos menores que
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- c++ compare char array
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- c++ push multiple elements to vector
- Category
- C++
- Title
- pow c++
- Category
- C++
- Title
- how to make string get spaces c++
- Category
- C++
- Title
- map vs unordered_map in C++
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- c++ clear stream
- Category
- C++
- Title
- free or delete in c++
- Category
- C++
- Title
- traverse map c++
- Category
- C++
- Title
- qt make widget ignore mouse events
- Category
- C++
- Title
- arduino falling edge
- Category
- C++
- Title
- c++ variable arguments
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- basic ex of maps in c++
- Category
- C++
- Title
- how to get a letter from the users string in c++
- Category
- C++
- Title
- namespaces c++
- Category
- C++
- Title
- Rectangle area hackerrank solution in c++
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- how to check sqrt of number is integer c++
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- how to output text in c++
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- how to check type in c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- user input c++
- Category
- C++
- Title
- Arrays hackerrank solution in c++
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- cpp loop through object
- Category
- C++
- Title
- insert at position in vector c++
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- c++ triple
- Category
- C++