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

Last Updated : 26 Oct, 2026

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.

Try It Yourself
redirect icon

Simple Traversal - O(n) Time and O(1) Space

The idea is to traverse the array once and count consecutive elements using a counter.

  • If the current element matches the previous one, increment the count.
  • Otherwise, update the maximum streak and reset the count.

Steps-By-Step Approach:

  • Initialize maxCount = 0 to store the maximum streak length and count = 1 for the current streak.
  • Start from index 1 and compare each element with the previous element.
  • If arr[i] == arr[i - 1], the current streak continues, so increment count.
  • If the elements are different, update maxCount with the current streak length and reset count = 1.
  • Continue until all elements have been checked.
  • After the loop, update maxCount one last time to include the final streak.
  • Return maxCount, which gives the maximum length of consecutive 0s or 1s.
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));

Output
4

Bit Manipulation - O(n) Time and O(1) Space

The idea is to use XOR (^) to check if consecutive elements are the same

  • Since XOR of two equal numbers is 0, if prev ^ num == 0, the sequence continues.
  • Otherwise, update the maximum streak and reset the count.

Steps to implement the above idea:

  • Initialize maxCount = 0 to store the maximum streak, count = 0 for the current streak, and prev = -1 to track the previous bit.
  • Traverse the array and use XOR to compare the current element num with prev.
  • If prev ^ num == 0, both bits are equal, so increment count to continue the streak.
  • If they differ, update maxCount with count and reset count = 1.
  • Update prev = num after each iteration to track the current element for the next comparison.
  • After the traversal, compare maxCount and count to include the final streak, and return the maximum.
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));

Output
4
Comment