Given a sorted binary array, efficiently count the total number of 1’s in it.

For example,

Input:  nums[] = [0, 0, 0, 0, 1, 1, 1]
 
Output: The total number of 1’s present is 3
 
 
Input:  nums[] = [0, 0, 1, 1, 1, 1, 1]
 
Output: The total number of 1’s present is 5

Practice this problem

A simple solution would be to run a linear search on the array and find the first occurrence of 1. The output will then be the array’s length minus the index of the first occurrence of 1. The problem with this approach is that its worst-case time complexity is O(n), where n is the size of the input.

 
We can easily solve this problem in O(log(n)) time using recursion by taking advantage of the fact that the input is sorted (i.e., all 0’s, followed by all 1’s). The idea is to split the array into two halves and recur for both halves. If the last element of the subarray is 0, then all 0’s are present in it since it is sorted, and we return 0 from the function. If the first array element is 1, then all its elements are 1’s only since the array is sorted, and we return the total number of elements in that partition.

The algorithm can be implemented as follows in C, C++, Java, and Python:

C


Download  Run Code

Output:

The total number of 1’s present is 3

C++


Download  Run Code

Output:

The total number of 1’s present is 3

Java


Download  Run Code

Output:

The total number of 1’s present is 3

Python


Download  Run Code

Output:

The total number of 1’s present is 3