Count 1's in a sorted binary array

Last Updated : 9 Feb, 2026

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 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

Try it on GfG Practice
redirect icon

[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 <iostream>
#include <vector>
using namespace std;

int countOnes(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
class GfG {

    static int countOnes(int[] arr) {
        int count = 0;
        for (int num : arr) {
            if (num == 1) count++;
            else break;
        }
        return count;
    }

    public static void main(String[] args) {
        int[] arr = {1, 1, 1, 1, 0, 0, 0};
        System.out.println(countOnes(arr));
    }
}
Python
def countOnes(arr):
    count = 0
    for num in arr:
        if num == 1:
            count += 1
        else:
            break
    return count

arr = [1, 1, 1, 1, 0, 0, 0]
print(countOnes(arr))
C#
using System;

class GfG
{
    static int countOnes(int[] arr)
    {
        int count = 0;
        foreach (int num in arr)
        {
            if (num == 1)
                count++;
            else
                break;
        }
        return count;
    }

    static void Main()
    {
        int[] arr = { 1, 1, 1, 1, 0, 0, 0 };
        Console.WriteLine(countOnes(arr));
    }
}
JavaScript
function countOnes(arr) {
    let count = 0;
    for (let num of arr) {
        if (num === 1) count++;
        else break;
    }
    return count;
}

const arr = [1, 1, 1, 1, 0, 0, 0];
console.log(countOnes(arr));

Output
4

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

Since the array is sorted with all 1s before 0s, there is a single transition point from 1 to 0. We use binary search to find the last occurrence of 1 by checking the middle element and narrowing the search space accordingly. The index of this last 1 plus one gives the count.

C++
#include <iostream>
#include <vector>
using namespace std;

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
import java.util.Arrays;

class GfG {
    public static int countOnes(int[] arr) {
        int n = arr.length;
        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;
    }

    public static void main(String[] args) {
        int[] arr = { 1, 1, 1, 1, 0, 0, 0 };
        System.out.println(countOnes(arr));
    }
}
Python
def countOnes(arr):
    n = len(arr)
    low, high = 0, n - 1
    
    # get the middle index
    while low <= high:
        mid = (low + high) // 2

        # If mid element is 0
        if arr[mid] == 0:
            high = mid - 1
            
        # If element is last 1
        elif mid == n - 1 or arr[mid + 1] != 1:
            return mid + 1
            
        # If element is not last 1    
        else:
            low = mid + 1
    return 0

#Custom Input
arr = [1, 1, 1, 1, 0, 0, 0]
print(countOnes(arr))
C#
using System;

class GfG
{
    public static int countOnes(int[] arr)
    {
        int n = arr.Length;
        int low = 0, high = n - 1;

        while (low <= high)
        {
            int mid = low + (high - low) / 2;

            // If mid element is 0, move left
            if (arr[mid] == 0)
            {
                high = mid - 1;
            }
            // If mid is the last 1
            else if (mid == n - 1 || arr[mid + 1] == 0)
            {
                return mid + 1;
            }
            // Otherwise, move right
            else
            {
                low = mid + 1;
            }
        }

        return 0;
    }

    static void Main(string[] args)
    {
        int[] arr = { 1, 1, 1, 1, 0, 0, 0 };
        Console.WriteLine(countOnes(arr));
    }
}
JavaScript
function countOnes(arr) {
    const n = arr.length;
    let low = 0, high = n - 1;
    
    // get the middle index
    while (low <= high) {
        const mid = Math.floor((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;
}

// Custom Input
const arr = [1, 1, 1, 1, 0, 0, 0];
console.log(countOnes(arr));

Output
4
Comment