Maximum consecutive one’s (or zeros) in a binary array
Last Updated :
25 Jun, 2025
Given a binary array arr[] consisting of only 0s and 1s, find the length of the longest contiguous sequence of either 1s or 0s in the array.
Examples :
Input: arr[] = [0, 1, 0, 1, 1, 1, 1]
Output: 4
Explanation: The maximum number of consecutive 1’s in the array is 4 from index 3-6.
Input: arr[] = [0, 0, 1, 0, 1, 0]
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.
[Approach - 1] Using Simple Traversal - O(n) Time and O(1) Space
The idea is to traverse the array once and count how many times the same value repeats consecutively. The thought process is to use a counter that increases when the current element matches the previous one. If a change is detected, we update the maximum streak and reset the count.
Steps-By-Step Approach:
- Initialize two variables: maxCount = 0 to store result and count = 1 for current streak length.
- Iterate through the array starting from index 1 to compare each element with the previous one.
- If arr[i] equals arr[i - 1], it means the streak continues, so increment count by 1.
- If they differ, update maxCount with the current count and reset count = 1 for a new streak.
- Continue this comparison until the loop finishes checking all elements in the array.
- After the loop, take the maximum of maxCount and count to cover the last streak.
- Return maxCount which holds the maximum number of consecutive 1s or 0s in the array.
C++
#include <iostream>
#include <vector>
using namespace std;
int maxConsecBits(vector<int> &arr) {
int maxCount = 0, count = 1;
// Loop through the array starting from the second element
for(int i = 1; i < arr.size(); i++) {
// If the current element is the same as the previous one
// increment the count
if(arr[i] == arr[i - 1]) {
count++;
}
else {
// If not equal, update maxCount if needed and reset count
maxCount = max(maxCount, count);
// Reset for a new sequence
count = 1;
}
}
return max(maxCount, count);
}
int main() {
vector<int> arr = {0, 1, 0, 1, 1, 1, 1};
cout << maxConsecBits(arr) << endl;
return 0;
}
Java
import java.util.List;
class GfG {
static int maxConsecBits(int[] arr){
if (arr.length == 0)
return 0;
int maxCount = 0, count = 1;
// Loop through the array starting from the second element
for (int i = 1; i < arr.length; i++) {
// If the current element is the same as the previous one
// increment the count
if (arr[i] == arr[i - 1]) {
count++;
}
// If not equal, update maxCount if needed and reset count
else {
maxCount = Math.max(maxCount, count);
count = 1;
}
}
return Math.max(maxCount, count);
}
public static void main(String[] args){
int[] arr = { 0, 1, 0, 1, 1, 1, 1 };
System.out.println(maxConsecBits(arr));
}
}
Python
def maxConsecBits(arr):
maxCount, count = 0, 1
# Loop through the array starting from the second element
for i in range(1, len(arr)):
# If the current element is the same as the previous one
# increment the count
if arr[i] == arr[i - 1]:
count += 1
# If not equal, update maxCount if needed and reset count
else:
maxCount = max(maxCount, count)
count = 1
return max(maxCount, count)
if __name__ == "__main__":
arr = [0, 1, 0, 1, 1, 1, 1]
print(maxConsecBits(arr))
C#
using System;
class GfG {
static int maxConsecBits(int[] arr) {
if (arr.Length == 0) {
return 0;
}
int maxCount = 0;
int count = 1;
// Loop through the array starting from the second element
for (int i = 1; i < arr.Length; i++) {
// If the current element is the same as the previous one
// increment the count
if (arr[i] == arr[i - 1]) {
count++;
}
// If not equal, update maxCount if needed and reset count
else {
maxCount = Math.Max(maxCount, count);
count = 1;
}
}
return Math.Max(maxCount, count);
}
public static void Main(string[] args) {
int[] arr = {0, 1, 0, 1, 1, 1, 1};
Console.WriteLine(maxConsecBits(arr));
}
}
JavaScript
function maxConsecBits(arr) {
let maxCount = 0, count = 1;
// Loop through the array starting from the second element
for (let i = 1; i < arr.length; i++) {
// If the current element is the same as the previous one
// increment the count
if (arr[i] === arr[i - 1]) {
count++;
}
// If not equal, update maxCount if needed and reset count
else {
maxCount = Math.max(maxCount, count);
count = 1;
}
}
return Math.max(maxCount, count);
}
// Driver Code
let arr = [0, 1, 0, 1, 1, 1, 1];
console.log(maxConsecBits(arr));
[Approach - 2] Using Bit Manipulation - O(n) Time and O(1) Space
The idea is to use XOR (^) to check if consecutive elements are the same. As XOR of two numbers is 0 if both numbers are same. So, If prev ^ num == 0, the sequence continues; else we update the maximum streak and reset the count.
Steps to implement the above idea:
- Initialize three variables: maxCount = 0, count = 0, and prev = -1 to track current and previous bits.
- Traverse the array, use XOR to compare current num with prev, if equal, increment count for the current streak.
- If prev and num differ, update maxCount with the current count, then reset count to 1.
- Update prev to current num in every iteration to track the previous element for the next comparison.
- After loop ends, compare and return the maximum of maxCount and count to cover last streak.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int maxConsecBits(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 = {0, 1, 0, 1, 1, 1, 1};
cout << maxConsecBits(arr) << endl;
return 0;
}
Java
import java.util.List;
class GfG {
static int maxConsecBits(int[] arr) {
if (arr.length == 0) return 0;
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 = {0, 1, 0, 1, 1, 1, 1};
System.out.println(maxConsecBits(arr));
}
}
Python
def maxConsecBits(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 = [0, 1, 0, 1, 1, 1, 1]
print(maxConsecBits(arr))
C#
using System;
class GfG {
static int maxConsecBits(int[] arr) {
if (arr.Length == 0) {
return 0;
}
int maxCount = 0;
int count = 0;
int 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(string[] args) {
int[] arr = {0, 1, 0, 1, 1, 1, 1};
Console.WriteLine(maxConsecBits(arr));
}
}
JavaScript
function maxConsecBits(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);
}
// Driver Code
let arr = [0, 1, 0, 1, 1, 1, 1];
console.log(maxConsecBits(arr));
Similar Reads
Maximum consecutive oneâs (or zeros) in a binary circular array Given a binary circular array of size N, the task is to find the count maximum number of consecutive 1âs present in the circular array.Examples: Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1} Output: 6 The last 4 and first 2 positions have 6 consecutive ones. Input: a[] = {0, 1, 0, 1, 0, 1, 0,
8 min read
Maximum Consecutive Ones After Flipping Zeroes Given a binary array arr[] and an integer k, find the maximum length of a subarray containing all ones after flipping at most k zeroes to 1's.Examples: Input: arr[] = {1, 0, 1}, k = 1Output: 3Explanation: By flipping the zero at index 1, all the array elements become one.Input: arr[] = {1, 0, 0, 1,
10 min read
Maximum number of consecutive 1's in binary representation of all the array elements Given an array arr[] of N elements, the task is to find the maximum number of consecutive 1's in the binary representation of an element among all the elements of the given array. Examples: Input: arr[] = {1, 2, 3, 4} Output: 2 Binary(1) = 01 Binary(2) = 10 Binary(3) = 11 Binary(4) = 100 Input: arr[
6 min read
Maximum Consecutive Zeroes in Concatenated Binary String You are given a binary string str of length n. Suppose you create another string of size n * k by concatenating k copies of str together. What is the maximum size of a substring of the concatenated string consisting only of 0's? Given that k > 1. Examples: Input : str = "110010", k = 2 Output : 2
8 min read
Length of second longest sequence of consecutive 1s in a binary array Given a binary array arr[] of size N, the task is to find the length of the second longest sequence of consecutive 1s present in the array. Examples: Input: arr[] = {1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0} Output: 4 3 Explanation: Longest sequence of consecutive ones is 4 i.e {arr[7], ... arr[10]}
7 min read
Maximum difference of zeros and ones in binary string Given a binary string of 0s and 1s. The task is to find the length of the substring which is having a maximum difference between the number of 0s and the number of 1s (number of 0s - number of 1s). In case of all 1s print -1. Examples: Input : S = "11000010001"Output : 6From index 2 to index 9, ther
15 min read