In this post, we will see how to count number of 1’s in sorted binary array.

## Problem

Print number of 1’s in a given sorted Binary Array.

For eg:-

output : 4

int[] arr = {0,0,1};

output : 1

## Solution

The Naive solution to solve this problem would be looping in the array and keep a count of 1’s occurring in the array.

But as the array is sorted we can stop at the very first one encountered, because as the array is sorted we can be sure that the element after first one would all be greater than or equal to ones, but as our array contains zeroes and ones only, therefore all elements after first one would be one. So our answer would be **(array.length – currentPointer)**. This will handle all the test cases and works in linear **O(n)** time.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package Arrays; import java.util.Scanner; public class num1sInsortedbinaryArray { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int[] arr = new int[scn.nextInt()]; for (int i = 0; i < arr.length; i++) { arr[i] = scn.nextInt(); } System.out.println(solve(arr)); } public static int solve(int[] arr) { int currPointer = 0; while (currPointer < arr.length) { if (arr[currPointer] == 1) { // as we have found our first one, we will stop here and // result would be (arr.length-currPinter) break; } // we will keep on increasing currPoniter as long as we are //encountering zeroes currPointer++; } return (arr.length - currPointer); } } |

We can still solve the problem more efficiently in logarithmic time i.e. with a worst time complexity of **O(log n)**.

**Efficient Approach :**

We can use** divide and conquer** approach, and move recursively at every step dividing the array into two subarrays virtually by keeping our start and end pointers.

- If the element at end pointer of a subarray is zero this means that all the elements in that array would be zeroes and we know the array is already sorted.
- What if the first element that is the value at start pointer of the array? this means that all the elements in that subarray are ones again keeping in mind that array is sorted.

Now let’s discuss the time complexity.

At every call we are basically dividing the number of elements to work upon into two halves.

Initially we had **N** elements, then **N/2**, then **N/4** and so on until we have one element left to work with.

if we denote **T(n)** for the time taken to process n elements.

Mathematically, we can write the equations as:

T(n/2) = T(n/4) + T(n/4)

.

.

.

T(2) = T(1) + T(1)

Say we have x such equations and as we move up in equations, number of elements are getting doubled, so eventually,

**n = 2^x**

taking log on both sides,

**x = log(n)** {to the base 2}

Hence the complexity of our algorithm comes out to be **O(log(n))**.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package org.ayush.java2blog; import java.util.Scanner; public class Num1sInSortedBinaryArray { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int[] arr = new int[scn.nextInt()]; for (int i = 0; i < arr.length; i++) { arr[i] = scn.nextInt(); } System.out.print("arr[]: {"); for (int i = 0; i < arr.length; i++) { System.out.print(" "+arr[i]); } System.out.println(" }"); System.out.println("Number of 1 in array :"+solveEfficient(0, arr.length-1, arr)); } public static int solveEfficient(int start, int end, int[] arr) { if (arr[start] == 1) { // start elem is one, hence all other elements will be one in // virtual subarr. return end - start + 1; } if (arr[end] == 0) { // end elem is zero this means, all previous elements of //subarr will be zeroes. return 0; } int mid = (start + end) / 2; int leftResult = solveEfficient(start, mid, arr); int rightResult = solveEfficient(mid + 1, end, arr); // divide the array into two virtual subHalves return leftResult + rightResult; } } |

When you run above program, you will get below output

arr[]: { 0 0 0 1 1 1 1 }

Number of 1 in array :4

That’s all about how to Count 1’s in sorted Binary Array

Comment in case of any doubt or edit. Happy Learning 🙂

#### Latest posts by Ayush Kumar (see all)

- Kruskal’s Algorithm for finding Minimum Spanning Tree – January 30, 2019
- Coin Change Problem in java – December 8, 2018
- Bellman Ford Algorithm in java – November 30, 2018

#### Join Our News Letter – Stay Updated

**Subscribe to Awesome Java Content**.