Maximum consecutive one’s (or zeros) in a binary array

Given binary array, find count of maximum number of consecutive 1’s present in the array.

Examples :

Input  : arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1}
Output : 4

Input  : arr[] = {0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}
Output : 1

A simple solution is consider every subarray and count 1’s in every subarray. Finally return return size of largest subarray with all 1’s.

An efficient solution is traverse array from left to right. If we see a 1, we increment count and compare it with maximum so far. If we see a 0, we reset count as 0.

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count maximum consecutive
// 1's in a binary array.
#include<bits/stdc++.h>
using namespace std;
  
// Returns count of maximum consecutive 1's
// in binary array arr[0..n-1]
int getMaxLength(bool arr[], int n)
{
    int count = 0; //intitialize count
    int result = 0; //initialize max
  
    for (int i = 0; i < n; i++)
    {
        // Reset count when 0 is found
        if (arr[i] == 0)
            count = 0;
  
        // If 1 is found, increment count
        // and update result if count becomes
        // more.
        else
        {
            count++;//increase count
            result = max(result, count);
        }
    }
  
    return result;
}
  
// Driver code
int main()
{
    bool arr[] = {1, 1, 0, 0, 1, 0, 1, 0,
                  1, 1, 1, 1};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << getMaxLength(arr, n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count maximum consecutive
// 1's in a binary array.
class GFG {
      
    // Returns count of maximum consecutive 1's
    // in binary array arr[0..n-1]
    static int getMaxLength(boolean arr[], int n)
    {
          
        int count = 0; //intitialize count
        int result = 0; //initialize max
      
        for (int i = 0; i < n; i++)
        {
              
            // Reset count when 0 is found
            if (arr[i] == false)
                count = 0;
      
            // If 1 is found, increment count
            // and update result if count becomes
            // more.
            else
            {
                count++;//increase count
                result = Math.max(result, count);
            }
        }
      
        return result;
    }
      
    // Driver method
    public static void main(String[] args)
    {
        boolean arr[] = {true, true, false, false
                         true, false, true, false,
                           true, true, true, true};
                             
        int n = arr.length;
          
        System.out.println(getMaxLength(arr, n));
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to count 
# maximum consecutive 1's 
# in a binary array.
  
# Returns count of maximum 
# consecutive 1's in binary
# array arr[0..n-1]
def getMaxLength(arr, n):
  
    # intitialize count
    count = 0 
      
    # initialize max
    result = 0 
  
    for i in range(0, n):
      
        # Reset count when 0 is found
        if (arr[i] == 0):
            count = 0
  
        # If 1 is found, increment count
        # and update result if count 
        # becomes more.
        else:
              
            # increase count
            count+= 1 
            result = max(result, count) 
          
    return result 
  
# Driver code
arr = [1, 1, 0, 0, 1, 0, 1
             0, 1, 1, 1, 1
n = len(arr) 
  
print(getMaxLength(arr, n))
  
# This code is contributed by Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count maximum 
// consecutive 1's in a binary array.
using System;
  
class GFG {
      
    // Returns count of maximum consecutive 
    // 1's in binary array arr[0..n-1]
    static int getMaxLength(bool []arr, int n)
    {
          
        int count = 0; //intitialize count
        int result = 0; //initialize max
      
        for (int i = 0; i < n; i++)
        {
              
            // Reset count when 0 is found
            if (arr[i] == false)
                count = 0;
      
            // If 1 is found, increment count
            // and update result if count 
            // becomes more.
            else
            {
                count++; //increase count
                result = Math.Max(result, count);
            }
        }
      
        return result;
    }
      
    // Driver code
    public static void Main()
    {
        bool []arr = {true, true, false, false
                      true, false, true, false,
                      true, true, true, true};
                              
        int n = arr.Length;
          
        Console.Write(getMaxLength(arr, n));
    }
}
  
// This code is contributed by Nitin Mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count maximum 
// consecutive 1's in a binary array.
  
// Returns count of maximum 
// consecutive 1's in binary 
// array arr[0..n-1]
function getMaxLength($arr, $n)
{
    // intitialize count
    $count = 0; 
      
    // initialize max
    $result = 0; 
  
    for ($i = 0; $i < $n; $i++)
    {
        // Reset count when 0 is found
        if ($arr[$i] == 0)
            $count = 0;
  
        // If 1 is found, increment 
        // count and update result
        // if count becomes more.
        else
        {
            // increase count
            $count++;
            $result = max($result, $count);
        }
    }
  
    return $result;
}
  
// Driver code
$arr = array(1, 1, 0, 0, 1, 0, 
             1, 0, 1, 1, 1, 1);
$n = sizeof($arr) / sizeof($arr[0]);
echo getMaxLength($arr, $n) ;
  
// This code is contributed by nitin mittal.
?>

chevron_right



Output :

4

Time Complexity : O(n)
Auxiliary Space : O(1)

Exercise:
Maximum consecutive zeros in a binary array.

This article is contributed by Smarak Chopdar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above”.



My Personal Notes arrow_drop_up

Improved By : nitin mittal



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.