Open In App

Count 1’s in a sorted binary array

Last Updated : 07 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 2
Explanation: Count of the 1’s in the given array is 2.

Input: arr[] = [1, 1, 1, 1, 1, 1, 1]
Output: 7

Input: arr[] = [0, 0, 0, 0, 0, 0, 0]
Output: 0

[Naive approach] Using linear Search – O(n) Time and O(1) Space

A simple solution is to linearly traverse the array until we find the 1’s in the array and keep count of 1s. If the array element becomes 0 then return the count of 1’s.

C++
#include <bits/stdc++.h>
using namespace std;

int countOnes(const vector<int> &arr) {
    int count = 0;
    for (int num : arr) {
        if (num == 1) count++;
        else break;
    }
    return count;
}

int main() {
    vector<int> arr = {1, 1, 1, 1, 0, 0, 0};
    cout << countOnes(arr);
    return 0;
}
Java Python C# JavaScript

Output
4

[Expected Approach] Using Binary Search – O(log n) Time and O(1) Space

We can optimize the count of leading 1s using Binary Search in O(log n) time. The approach is:

  1. Find the mid using low and high to find the last occurrence of 1.
  2. If mid element is 0, move to the left half.
  3. Else If mid contains 1 and the next element is 0 (or it’s the last element), return mid + 1 as the count.
  4. Else search the right half.
C++
#include <bits/stdc++.h>
using namespace std;

/* Returns counts of 1's in arr[low..high]. 
   The array is assumed to be sorted in 
   non-increasing order */
int countOnes(vector<int> &arr)
{   
    int n = arr.size();
    int ans = 0;
    int low = 0, high = n - 1;
    
    // get the middle index
    while (low <= high) { 
        int mid = (low + high) / 2;

        // If mid element is 0
        if (arr[mid] == 0)
            high = mid - 1;
            
        // If element is last 1
        else if (mid == n - 1 || arr[mid + 1] != 1)
            return mid + 1;
            
        // If element is not last 1    
        else
            low = mid + 1;
    }
    return 0;
}

int main()
{
    vector<int> arr = { 1, 1, 1, 1, 0, 0, 0 };
    cout << countOnes(arr);
    return 0;
}
Java Python C# JavaScript

Output
4

[Alternate Approach] Using inbuilt functions

In C++, Java, and C#, we can use inbuilt functions that take only O(log n) time. However, in Python, we can apply binary search using inbuilt functions on non-decreasing elements, but it will take O(log n) time, not O(n). JavaScript does not provide a built-in binary search function.

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{

    vector<int> arr = {1, 1, 1, 1, 0, 0, 0, 0, 0};
    int size = arr.size();

    // Iterayot to the first occurrence of one
    auto ptr = lower_bound(arr.begin(), arr.end(), 0, greater<int>());
    cout << (ptr - arr.begin());

    return 0;
}
Java Python C# JavaScript

Output
4




Next Article

Similar Reads