- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# Javascript Program to Count 1’s in a sorted binary array

We have two approaches to count 1’s in a sorted binary array. The first one is to iterate through the array and count the 1's. The second approach is to use a binary search algorithm to find the first occurrence of 1 in the array.

It is important to note that in order to use these approaches, the array must be sorted.

In this blog post, we will discuss a JavaScript program to count the number of 1's in a sorted binary array. We will also look at some edge cases and optimization techniques to make the program more efficient.

*Problem Statement *

Given a sorted binary array, the task is to count the number of 1's in the array. The array can be of any size and the elements can only be either 0 or 1.

### Input

bin_array[] = {0, 0, 0,1,1,1}

### Output

3

## Approach 1

The first approach that comes to mind is to iterate through the array and count the number of 1's.

Initialize a count variable to store the number of ones in the array.

Iterate through the array and check each element. If the current element is equal to 1, increase the counter.

### Example

<html> <body> <p id="result1"></p> <p id="result2"></p> <script> function count_num_of_Ones( bin_array,n) { let num_of_ones=0; for (let ind = 0; ind < n; ind++) { if(bin_array[ind]==1){ num_of_ones++; } } return num_of_ones; } let bin_array = [0,0,0,1,1,1]; let n = bin_array.length; document.getElementById("result1").innerHTML = "Original Array: " + JSON.stringify(bin_array); document.getElementById("result2").innerHTML = "Count of 1's in given array is " + count_num_of_Ones(bin_array,n) </script> </body> </html>

However, this approach has a time complexity of O(n) where n is the size of the array, because we are traversing the complete array once.

This can be optimized by taking advantage of the fact that the array is sorted.

## Approach 2

To find the first instance of 1 in the array, use a binary search approach. Simply subtracting the index of the first instance of 1 from the total number of items in the array will yield the number of 1s.

In this implementation, we are using the "first occurrence" binary search technique to find the first instance of 0 in the array.

The low and high variables are initially set to the first and last indexes of the array, accordingly.

The number of items in the array is also specified as the value of a variable named firstOne, which will be used to record the index of the first instance of the number 1.

Until the low index is greater than the high value or equal to it, the while loop will keep running. After each iteration, we determine the midpoint of the current range.

If the element at the midway is 1, the firstOne variable is updated and the high index is moved to the earlier element. If the element at the halfway point is 0, we move the low index to the subsequent element.

Once the while loop is complete, we check to see if the firstOne variable corresponds to the array's -1 value. If it is, it means that there are no 1s in the array, and we return 1. If not, firstOne is returned less arr.length.

### Example

<html> <body> <p id="result1"></p> <p id="result2"></p> <script> function count_num_of_Ones(bin_arr,low,high) { var low = 0; var high = bin_arr.length - 1; var firstOne = -1; while (low <= high) { var mid = Math.floor((low + high) / 2); if (bin_arr[mid] == 1) { firstOne = mid; high = mid -1; } else { low = mid + 1; } } return firstOne == -1 ? 0 : bin_arr.length - firstOne; } let bin_array = [0,0,0,1,1,1,1]; let n = bin_array.length; document.getElementById("result1").innerHTML = "Original Array: " + JSON.stringify(bin_array); document.getElementById("result2").innerHTML = "Count of 1's in given array is " + count_num_of_Ones(bin_array,n) </script> </body> </html>

The time complexity of this approach is O(log n) which is much more efficient than the previous approach.

In this tutorial, we have discussed a JavaScript program to count the number of 1's in a sorted binary array.

- Related Articles
- Count 1’s in a sorted binary array in C++
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Count subarrays consisting of only 0’s and only 1’s in a binary array in C++
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Finding maximum number of consecutive 1's in a binary array in JavaScript
- JavaScript Program for Ceiling in a sorted array
- Count Binary String without Consecutive 1's
- Absolute distinct count in a sorted array?
- Given a sorted array of 0’s and 1’s, find the transition point of the array in C++
- Convert Sorted Array to Binary Search Tree in Python
- Absolute distinct count in a sorted array in C++?
- Find the index of first 1 in a sorted array of 0's and 1's in C++
- JavaScript Program for the Last duplicate element in a Sorted Array
- Merge two sorted arrays to form a resultant sorted array in JavaScript