Find number of 1’s in a sorted binary array

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

For example,


Input:  A[] = [0, 0, 0, 0, 1, 1, 1]
Output: Total number of 1’s present are 3

Input:  A[] = [0, 0, 1, 1, 1, 1, 1]
Output: Total number of 1’s present are 5

 


 

A simple solution would be to run a linear search on the array and find the first occurrence of one. Then the output will be number of elements in the array minus index of the first occurrence of one. The problem with this approach is that its worst case time complexity is O(n).

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

C++

Download   Run Code

Output:

Total number of 1’s present are 3

 
Time complexity of above solution is O(log(n)).
Auxiliary space used by the program is O(log(n)) for recursive function.

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



 

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

For example,


Input:  A[] = [0, 0, 0, 0, 1, 1, 1]
Output: Total number of 1’s present are 3

Input:  A[] = [0, 0, 1, 1, 1, 1, 1]
Output: Total number of 1’s present are 5

 


 

A simple solution would be to run a linear search on the array and find the first occurrence of one. Then the output will be number of elements in the array minus index of the first occurrence of one. The problem with this approach is that its worst case time complexity is O(n).

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

C++

Download   Run Code

Output:

Total number of 1’s present are 3

 
Time complexity of above solution is O(log(n)).
Auxiliary space used by the program is O(log(n)) for recursive function.

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



 

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

For example,


Input:  A[] = [0, 0, 0, 0, 1, 1, 1]
Output: Total number of 1’s present are 3

Input:  A[] = [0, 0, 1, 1, 1, 1, 1]
Output: Total number of 1’s present are 5

 


 

A simple solution would be to run a linear search on the array and find the first occurrence of one. Then the output will be number of elements in the array minus index of the first occurrence of one. The problem with this approach is that its worst case time complexity is O(n).

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

C++

Download   Run Code

Output:

Total number of 1’s present are 3

 
Time complexity of above solution is O(log(n)).
Auxiliary space used by the program is O(log(n)) for recursive function.

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



 



Leave a Reply

Notify of
avatar
Sort by:   newest | oldest | most voted
Man Singh Arya
Guest
Man Singh Arya

There are log(n) Auxiliary space used for recursion function.

Man Singh Arya
Guest
Man Singh Arya

There is the problem in your site. It remove my half code during comment post.

Man Singh Arya
Guest
Man Singh Arya
int count( int n, int arr[] ) { int left=0, right=n-1, mid, pos=-1; while( left<=right ) { mid = left +(right-left)/2; if( arr[mid] == 1 ) { pos = mid; right = mid-1; } else left = mid+1; } return (n-pos); } //——————————————— if( arr[0] == 1 ) cout<<n; else… Read more »
wpDiscuz