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.

Updated on: 06-Mar-2023

282 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements