# Count Ones in a Sorted binary array in Java

A sorted binary array is given (an array that only contains 0's and 1's is a binary array). The task is to find the number of ones that are present in the binary sorted array.

Example: 1

Input:

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

Output: 5

Explanation: Total number of ones available in the input array is 5.

Example: 2

Input:

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

Output: 0

Explanation: Total number of ones available in the input array is 0.

Naïve Approach

In this approach, we will use a variable, which is countOne in our case, and will initialize it to zero. Now, we will use a loop that iterates over the array elements. Whenever the current element pointed out by the loop variable is 1, we increment the countOne variable by 1. When the iteration over the array elements is over, the value of the variable countOne is our answer. Observe the following implementation.

FileName: CountOneSortedArray.java

Output:

```For the input array:
0 0 0 0 1 1 1 1 1
The total number of ones is: 5

For the input array:
0 0 0 0 0 0 0 0 0
The total number of ones is: 0
```

Complexity Analysis: Since the program is using a for-loop, the time complexity of the program is O(n), where n is the total number of elements present in the input array. The space complexity of the program is constant, i.e., O(1).

Let's optimize the above program by traversing the array from right to left. It is because the array is sorted, and all the ones elements that are present will reside on the right side. Thus, we will traverse only those elements that contain the value 1, and then will terminate the loop using the break statement. In most of the scenarios, the following implementation works better than the above implementation. There is only one scenario when the following and the above implementation works with the same time consumption, and that is when all the elements of the input array is of the value 1.

FileName: CountOneSortedArray1.java

Output:

```For the input array:
0 0 0 0 1 1 1 1 1
The total number of ones is: 5

For the input array:
0 0 0 0 0 0 0 0 0
The total number of ones is: 0
```

Complexity Analysis: The loop used in the above program will traverse only those elements that are ones. Thus, the time complexity of the program is O(p), where p is the total number of ones present in the sorted binary array. The space complexity of the program is the same as the previous program.

## Approach: Using Binary Search

Since the input array is sorted, one can use the binary search to find the total number of ones present in the input array.

FileName: CountOneSortedArray.java

Output:

```For the input array:
0 0 0 0 1 1 1 1 1
The total number of ones is: 5

For the input array:
0 0 0 0 0 0 0 0 0
The total number of ones is: 0
```

Complexity Analysis: The program is using the binary search making the time complexity of the program as O(log(n)), where n is the total number of elements present in the input array. The space complexity of the program is the same as the previous program.

### Feedback   