Javascript Program to Count 1's in a sorted binary array
Last Updated :
16 Sep, 2024
Given a binary array sorted in non-increasing order, count the number of 1's in it.
Examples:
Input: arr[] = {1, 1, 0, 0, 0, 0, 0}
Output: 2
Input: arr[] = {1, 1, 1, 1, 1, 1, 1}
Output: 7
Input: arr[] = {0, 0, 0, 0, 0, 0, 0}
Output: 0
A simple solution is to traverse the array linearly. The time complexity of the simple solution is O(n). We can use Binary Search to find count in O(Logn) time. The idea is to look for the last occurrence of 1 using Binary Search. Once we find the index last occurrence, we return index + 1 as count.
The following is the implementation of the above idea.
JavaScript
// Javascript program to count one's in a boolean array
/* Returns counts of 1's in arr[low..high]. The array is
assumed to be sorted in non-increasing order */
function countOnes(arr, low, high) {
if (high >= low) {
// get the middle index
let mid = Math.trunc(low + (high - low) / 2);
// check if the element at middle index is last 1
if ((mid == high || arr[mid + 1] == 0) && (arr[mid] == 1))
return mid + 1;
// If element is not last 1, recur for right side
if (arr[mid] == 1)
return countOnes(arr, (mid + 1), high);
// else recur for left side
return countOnes(arr, low, (mid - 1));
}
return 0;
}
// Driver program
let arr = [1, 1, 1, 1, 0, 0, 0];
let n = arr.length;
console.log("Count of 1's in given array is " + countOnes(arr, 0, n - 1));
OutputCount of 1's in given array is 4
Complexity Analysis:
- Time complexity: O(Logn).
- Space complexity: o(log n) (function call stack)
The same approach with iterative solution would be
JavaScript
/* Returns counts of 1's in arr[low..high]. The array is
assumed to be sorted in non-increasing order */
function countOnes(arr, n) {
let ans;
let low = 0, high = n - 1;
while (low <= high) { // get the middle index
let mid = Math.floor((low + high) / 2);
// else recur for left side
if (arr[mid] < 1)
high = mid - 1;
// If element is not last 1, recur for right side
else if (arr[mid] > 1)
low = mid + 1;
else
// check if the element at middle index is last 1
{
if (mid == n - 1 || arr[mid + 1] != 1)
return mid + 1;
else
low = mid + 1;
}
}
}
let arr = [1, 1, 1, 1, 0, 0, 0];
let n = arr.length;
console.log("Count of 1's in given array is " + countOnes(arr, n));
// This code is contributed by unknown2108
OutputCount of 1's in given array is 4
Complexity Analysis:
- Time complexity: O(Logn)
- Space complexity: O(1)
Please refer complete article on Count 1's in a sorted binary array for more details!
Similar Reads
Count 1's in a sorted binary array Given a binary array arr[] of size n, which is sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = [1, 1, 0, 0, 0, 0, 0]Output: 2Explanation: Count of the 1's in the given array is 2.Input: arr[] = [1, 1, 1, 1, 1, 1, 1]Output: 7Input: arr[] = [0, 0, 0, 0, 0, 0, 0]
7 min read
Javascript Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0
5 min read
Find the transition point in a binary array Given a sorted array, arr[], containing only 0s and 1s, find the transition point, i.e., the first index where 1 was observed, and before that, only 0 was observed. If arr does not have any 1, return -1. If the array does not have any 0, return 0.Examples : Input: 0 0 0 1 1Output: 3Explanation: Inde
7 min read
Find the index of first 1 in a sorted array of 0's and 1's Given a sorted array consisting 0's and 1's. The problem is to find the index of first '1' in the sorted array. It could be possible that the array consists of only 0's or only 1's. If 1's are not present in the array then print "-1". Examples : Input : arr[] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1}Output :
15 min read
Count of 1's after flipping the bits at multiples from 1 to N Given N sized binary array A[] containing all 0's initially. The task is to find the final count of 1's after flipping the bits at multiples from 1 to N. Examples: Input: A[] = [0, 0, 0, 0]Output: 2Explanation: Flipping bits at multiples of 1 - [1, 1, 1, 1]Flipping bits at multiples of 2 - [1, 0, 1,
9 min read
Count of elements that are binary searchable in the given array Given an array arr[] consisting of N integers, the task is to find the maximum count of integers that are binary searchable in the given array. Examples: Input: arr[] = {1, 3, 2}Output: 2Explanation: arr[0], arr[1] can be found. Input: arr[] = {3, 2, 1, 10, 23, 22, 21}Output: 3Explanation: arr[1], a
13 min read