# 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 recurse 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.

## Java

Output:

Total number of 1’s present are 3

The time complexity of above solution is O(log(n)) and auxiliary space used by the program is O(log(n)) for recursive function.

(1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

### Leave a Reply

Subscribe
Notify of
Guest
Madhav Rathi

This can be done using
upper_bound(a.begin(), a.end(), 1) - lower_bound(a.begin(), a.end(), 1)
HEADER FILE:

Guest
Man Singh Arya

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

Guest
Man Singh Arya

Guest
arandomguy

You are using “First Occurence Algorithm” which is fine too.

Guest

The java code gets StackOverflow with {0,0,0,1} input. One way to solve this is by changing the last two lines as below
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.techiedelight.com/find-number-1s-sorted-binary-array/", "fetch_time": 1553387314000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2019-13/segments/1552912203123.91/warc/CC-MAIN-20190324002035-20190324024035-00333.warc.gz", "warc_record_offset": 909908816, "warc_record_length": 25639, "token_count": 542, "char_count": 2000, "score": 3.5, "int_score": 4, "crawl": "CC-MAIN-2019-13", "snapshot_type": "latest", "language": "en", "language_score": 0.8801367878913879, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00026-of-00064.parquet"}