Maximum consecutive one’s (or zeros) in a binary array
Given an array arr[] consisting of only 0’s and 1’s, the task is to find the count of a maximum number of consecutive 1’s or 0’s present in the array.
Examples :
Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1}
Output: 4
Explanation: The maximum number of consecutive 1’s in the array is 4 from index 8-11.Input: arr[] = {0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}
Output: 2
Explanation: The maximum number of consecutive 0’s in the array is 2 from index 0-1.Input: arr[] = {0, 0, 0, 0}
Output: 4
Explanation: The maximum number of consecutive 0’s in the array is 4.
Table of Content
Using Simple Traversal – O(n) Time and O(1) Space
The idea is to traverse the array while keeping track of the current streak of consecutive 1s or 0s. If the sequence is broken, update the maximum count and reset the current count.
Steps to implement the above idea:
- Initialize
maxCount
andcount
to0
and track theprevious
element. - Traverse the list, increment
count
if the current element matchesprevious
, else updatemaxCount
and resetcount
. - Update
previous
to the current element in each iteration. - Return the maximum of
maxCount
andcount
at the end.
// C++ program to find the maximum number
// of consecutive 1s or 0s
#include <bits/stdc++.h>
using namespace std;
int maxConsecutiveCount(vector<int> &arr) {
int maxCount = 0, count = 1;
for(int i = 1; i < arr.size(); i++) {
if(arr[i] == arr[i - 1]) {
count++;
} else {
maxCount = max(maxCount, count);
count = 1;
}
}
return max(maxCount, count);
}
int main() {
vector<int> arr = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1};
cout << maxConsecutiveCount(arr) << endl;
return 0;
}
// Java program to find the maximum number of consecutive 1s or 0s
import java.util.List;
class GfG {
static int maxConsecutiveCount(int[] arr) {
if (arr.length == 0) return 0; // Handle edge case
int maxCount = 0, count = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == arr[i - 1]) {
count++;
} else {
maxCount = Math.max(maxCount, count);
count = 1;
}
}
return Math.max(maxCount, count);
}
public static void main(String[] args) {
int[] arr = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1};
System.out.println(maxConsecutiveCount(arr));
}
}
# Python program to find the maximum number of consecutive 1s or 0s
def maxConsecutiveCount(arr):
maxCount, count = 0, 1
for i in range(1, len(arr)):
if arr[i] == arr[i - 1]:
count += 1
else:
maxCount = max(maxCount, count)
count = 1
return max(maxCount, count)
if __name__ == "__main__":
arr = [1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1]
print(maxConsecutiveCount(arr))
// C# program to find the maximum number of consecutive 1s or 0s
using System;
using System.Collections.Generic;
class GfG {
public static int MaxConsecutiveCount(List<int> arr) {
int maxCount = 0, count = 1;
for (int i = 1; i < arr.Count; i++) {
if (arr[i] == arr[i - 1]) {
count++;
} else {
maxCount = Math.Max(maxCount, count);
count = 1;
}
}
return Math.Max(maxCount, count);
}
public static void Main() {
List<int> arr = new List<int> {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1};
Console.WriteLine(MaxConsecutiveCount(arr));
}
}
// JavaScript program to find the maximum number of consecutive 1s or 0s
function maxConsecutiveCount(arr) {
let maxCount = 0, count = 1;
for (let i = 1; i < arr.length; i++) {
if (arr[i] === arr[i - 1]) {
count++;
} else {
maxCount = Math.max(maxCount, count);
count = 1;
}
}
return Math.max(maxCount, count);
}
let arr = [1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1];
console.log(maxConsecutiveCount(arr));
Output
4
Using Bit Manipulation – O(n) Time and O(1) Space
The idea is to use XOR (^) to check if consecutive elements are the same. If prev ^ num == 0, the sequence continues; otherwise, reset the count.
Steps to implement the above idea:
- Initialize
maxCount
,count
, and setprevious
to-1
. - Traverse the list, use XOR (
^
) to check if the current element matchesprevious
. - If the result is
0
, incrementcount
; otherwise, updatemaxCount
and resetcount
. - At the end, return the maximum of
maxCount
andcount
.
Below is the implementation of the above approach:
// C++ program using bit manipulation to find max consecutive 1s or 0s
#include <bits/stdc++.h>
using namespace std;
int maxConsecutiveCount(vector<int> &arr) {
int maxCount = 0, count = 0, prev = -1;
for (int num : arr) {
// If the current number is the same as the previous number
if ((prev ^ num) == 0) {
count++;
} else {
// Update maxCount and reset count
maxCount = max(maxCount, count);
count = 1;
}
prev = num;
}
return max(maxCount, count);
}
int main() {
vector<int> arr = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1};
cout << maxConsecutiveCount(arr) << endl;
return 0;
}
// Java program using bit manipulation to find max consecutive 1s or 0s
import java.util.List;
class GfG {
static int maxConsecutiveCount(int[] arr) {
if (arr.length == 0) return 0; // Handle edge case
int maxCount = 0, count = 0, prev = -1;
for (int num : arr) {
// If the current number is the same as the previous number
if ((prev ^ num) == 0) {
count++;
} else {
// Update maxCount and reset count
maxCount = Math.max(maxCount, count);
count = 1;
}
prev = num;
}
return Math.max(maxCount, count);
}
public static void main(String[] args) {
int[] arr = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1};
System.out.println(maxConsecutiveCount(arr));
}
}
# Python program using bit manipulation to find max consecutive 1s or 0s
def maxConsecutiveCount(arr):
maxCount, count, prev = 0, 0, -1
for num in arr:
# If the current number is the same as the previous number
if (prev ^ num) == 0:
count += 1
else:
# Update max_count and reset count
maxCount = max(maxCount, count)
count = 1
prev = num
return max(maxCount, count)
if __name__ == "__main__":
arr = [1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1]
print(maxConsecutiveCount(arr))
// C# program using bit manipulation to find max consecutive 1s or 0s
using System;
using System.Collections.Generic;
class GfG {
public static int MaxConsecutiveCount(List<int> arr) {
int maxCount = 0, count = 0, prev = -1;
foreach (int num in arr) {
// If the current number is the same as the previous number
if ((prev ^ num) == 0) {
count++;
} else {
// Update maxCount and reset count
maxCount = Math.Max(maxCount, count);
count = 1;
}
prev = num;
}
return Math.Max(maxCount, count);
}
public static void Main() {
List<int> arr = new List<int> {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1};
Console.WriteLine(MaxConsecutiveCount(arr));
}
}
// JavaScript program using bit manipulation to find max consecutive 1s or 0s
function maxConsecutiveCount(arr) {
let maxCount = 0, count = 0, prev = -1;
for (let num of arr) {
// If the current number is the same as the previous number
if ((prev ^ num) === 0) {
count++;
} else {
// Update maxCount and reset count
maxCount = Math.max(maxCount, count);
count = 1;
}
prev = num;
}
return Math.max(maxCount, count);
}
// Example usage
let arr = [1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1];
console.log(maxConsecutiveCount(arr));
Output
4