Find Maximum Consecutive Ones in Array using Loop - Optimal Algorithm

Problem Statement

You are given a binary array that consists only of 0s and 1s. Your task is to find the maximum number of consecutive 1s that appear in the array.

In simpler terms, you need to count the longest stretch of 1s that are placed next to each other (without any 0s in between).

If the array is empty, the result should be 0.

Examples

Input Array Max Consecutive 1s Description
[1, 1, 0, 1, 1, 1] 3 The longest sequence of 1s is [1, 1, 1]Visualization
[1, 1, 1, 1] 4 All elements are 1s, so the count is the length of the arrayVisualization
[0, 0, 0] 0 No 1s in the arrayVisualization
[1, 0, 1, 0, 1] 1 All 1s are isolated by 0sVisualization
[0, 1, 1, 0, 1, 1, 1, 0] 3 There are two sequences: [1, 1] and [1, 1, 1]; the longest is length 3Visualization
[1] 1 Single element that is 1Visualization
[0] 0 Single element that is 0Visualization
[] 0 Empty array should return 0Visualization

Visualization Player

Solution

To solve this problem, we need to count how many 1s appear consecutively in the array. Whenever we encounter a 0, we know the streak is broken and we reset our count.

Let’s break it down:

  • We start scanning the array from the beginning.
  • Each time we see a 1, we increase a temporary counter (say current_count).
  • If we see a 0, that means the current sequence has ended, so we reset current_count to 0.
  • Throughout the loop, we also keep track of the highest value current_count has reached — that’s our max_count.

Let’s understand different scenarios:

  • Normal case: If the array has both 1s and 0s, we look for the longest continuous stretch of 1s between 0s. For example, in [1, 1, 0, 1, 1, 1], the answer is 3.
  • All 1s: If the array contains only 1s (like [1, 1, 1, 1]), then the entire array is one long stretch — the answer is the length of the array.
  • All 0s: If the array contains only 0s, there are no 1s, so the answer is 0.
  • Isolated 1s: If every 1 is surrounded by 0s (like [1, 0, 1]), then the maximum stretch is just 1.
  • Empty array: If the array has no elements, then clearly there are no 1s to count — we return 0.

This method uses a single loop and no extra space, making it extremely efficient. It works in O(n) time where n is the number of elements in the array.

Algorithm Steps

  1. Given an array arr that contains only 0 and 1.
  2. Initialize two counters: max_count = 0 and current_count = 0.
  3. Iterate through the array:
  4. → If the current element is 1, increment current_count.
  5. → If the current element is 0, reset current_count to 0.
  6. After each step, update max_count if current_count is greater.
  7. After the loop, return max_count.

Code

Python
JavaScript
Java
C++
C
def max_consecutive_ones(arr):
    max_count = 0
    current_count = 0
    for num in arr:
        if num == 1:
            current_count += 1
            max_count = max(max_count, current_count)
        else:
            current_count = 0
    return max_count

# Sample Input
arr = [1, 1, 0, 1, 1, 1]
print("Max Consecutive Ones:", max_consecutive_ones(arr))

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(n)Even if the array starts with a long streak of 1s, we still need to check all elements to ensure it's the longest sequence.
Average CaseO(n)The algorithm goes through each element of the array once in a single pass, updating counters as needed.
Worst CaseO(n)In the worst case, such as alternating 1s and 0s, every element must still be processed to determine the maximum streak.

Space Complexity

O(1)

Explanation: The algorithm uses only two variables to keep track of the current and maximum count. No additional space is required.