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 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.
Implementation:
CPP
#include<bits/stdc++.h>
using namespace std;
int getMaxLength( bool arr[], int n)
{
int count = 0;
int result = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == 0)
count = 0;
else
{
count++;
result = max(result, count);
}
}
return result;
}
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;
}
|
Java
class GFG {
static int getMaxLength( boolean arr[], int n)
{
int count = 0 ;
int result = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] == false )
count = 0 ;
else
{
count++;
result = Math.max(result, count);
}
}
return result;
}
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));
}
}
|
Python3
def getMaxLength(arr, n):
count = 0
result = 0
for i in range ( 0 , n):
if (arr[i] = = 0 ):
count = 0
else :
count + = 1
result = max (result, count)
return result
arr = [ 1 , 1 , 0 , 0 , 1 , 0 , 1 ,
0 , 1 , 1 , 1 , 1 ]
n = len (arr)
print (getMaxLength(arr, n))
|
C#
using System;
class GFG {
static int getMaxLength( bool []arr, int n)
{
int count = 0;
int result = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == false )
count = 0;
else
{
count++;
result = Math.Max(result, count);
}
}
return result;
}
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));
}
}
|
PHP
<?php
function getMaxLength( $arr , $n )
{
$count = 0;
$result = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] == 0)
$count = 0;
else
{
$count ++;
$result = max( $result , $count );
}
}
return $result ;
}
$arr = array (1, 1, 0, 0, 1, 0,
1, 0, 1, 1, 1, 1);
$n = sizeof( $arr ) / sizeof( $arr [0]);
echo getMaxLength( $arr , $n ) ;
?>
|
Javascript
<script>
function getMaxLength(arr, n) {
let count = 0;
let result = 0;
for (let i = 0; i < n; i++) {
if (arr[i] == 0)
count = 0;
else {
count++;
result = Math.max(result, count);
}
}
return result;
}
let arr = new Array(1, 1, 0, 0, 1, 0,
1, 0, 1, 1, 1, 1);
let n = arr.length;
document.write(getMaxLength(arr, n));
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(1)
Approach 2:
Another approach to solve this problem is to use the bitwise AND operation to count the number of consecutive 1’s in the binary representation of the given number. We iterate over the bits of the number from the right and use a mask to check the value of each bit. If the bit is 1, we increment a counter. If the bit is 0, we reset the counter to zero. We update the maxCount if the counter is greater than maxCount.
Here’s the implementation of the above approach in C++:
C++
#include <iostream>
#include <vector>
using namespace std;
int maxConsecutiveOnes(vector< int >& nums) {
int max_count = 0, current_count = 0, mask = 0;
for ( int i = 0; i < nums.size(); i++) {
if (nums[i] == 1) {
mask = (mask << 1) | 1;
} else {
mask = mask << 1;
}
if ((nums[i] & mask) != 0) {
current_count++;
} else {
max_count = max(max_count, current_count);
current_count = 0;
mask = 0;
}
}
max_count = max(max_count, current_count);
return max_count;
}
int main() {
vector< int > nums = {1, 1, 0, 0, 1, 0, 1, 0,
1, 1, 1, 1};
int max_ones = maxConsecutiveOnes(nums);
cout << "Maximum consecutive ones: " << max_ones << endl;
return 0;
}
|
OutputMaximum consecutive ones: 4
Time Complexity: O(logn), where n is the decimal representation of the given number.
Space Complexity: 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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.