Count 1's in a sorted binary array

Last Updated : 29 Sep, 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
Explanation: All elements in the array are 1.

Input: arr[] = [0, 0, 0, 0, 0, 0, 0]
Output: 0
Explanation: The array contains no 1s.

Try It Yourself
redirect icon

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

Since all the 1s appear before the 0s, traverse the array from left to right and count each 1.

  • If the current element is 1, increment the count.
  • As soon as a 0 is found, stop the traversal because all remaining elements are also 0.
  • Return the count of 1s.
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;
}
C
#include <stdio.h>

int countOnes(int arr[], int n) {
    int count = 0;

    for (int i = 0; i < n; i++) {
        if (arr[i] == 1)
            count++;
        else
            break;
    }

    return count;
}

int main() {
    int arr[] = {1, 1, 1, 1, 0, 0, 0};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("%d", countOnes(arr, n));

    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 contains all 1s followed by all 0s, the position of the last 1 can be found using Binary Search.

  • If arr[mid] is 0, the last 1 must be on the left side, so move high to mid - 1.
  • If arr[mid] is 1, check whether it is the last 1.
  • If mid is the last index or arr[mid + 1] is 0, then mid + 1 is the count of 1s.
  • Otherwise, the last 1 lies on the right side, so move low to mid + 1.

Consider the input array: arr[] = [1, 1, 1, 1, 0, 0, 0]

  • Initially, low = 0 and high = 6.
  • Calculate mid = (0 + 6) / 2 = 3.
  • arr[3] is 1.
  • The next element, arr[4], is 0, so arr[3] is the last 1.
  • Therefore, return mid + 1 = 4.

Hence, the array contains 4 ones.

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;
}
C
#include <stdio.h>

int countOnes(int arr[], int n)
{
    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()
{
    int arr[] = {1, 1, 1, 1, 0, 0, 0};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("%d", countOnes(arr, n));

    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