Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Max Consecutive Ones

Difficulty: Easy Source: NeetCode

Problem

Given a binary array nums, return the maximum number of consecutive 1s in the array.

Example 1: Input: nums = [1, 1, 0, 1, 1, 1] Output: 3

Example 2: Input: nums = [1, 0, 1, 1, 0, 1] Output: 2

Constraints:

  • 1 <= nums.length <= 10^5
  • nums[i] is either 0 or 1

Prerequisites

Before attempting this problem, you should be comfortable with:

  • Arrays — understanding how to traverse and access array elements sequentially
  • Basic Iteration — using loops to scan through elements and track state with counters

1. Brute Force

Intuition

For each position in the array, count how many consecutive 1s start from that position. Scan forward until hitting a 0 or the end of the array, then track the maximum count seen. This straightforward approach checks every possible starting position.

Algorithm

  1. Initialize res = 0 to track the maximum consecutive ones.
  2. For each starting index i:
    • Initialize a counter cnt = 0.
    • Scan forward from i while the current element is 1, incrementing cnt.
    • Stop when encountering a 0 or reaching the end.
    • Update res = max(res, cnt).
  3. Return res.

Solution

def findMaxConsecutiveOnes(nums):
    n, res = len(nums), 0

    for i in range(n):
        cnt = 0
        for j in range(i, n):
            if nums[j] == 0:
                break
            cnt += 1
        res = max(res, cnt)

    return res


print(findMaxConsecutiveOnes([1, 1, 0, 1, 1, 1]))  # 3
print(findMaxConsecutiveOnes([1, 0, 1, 1, 0, 1]))  # 2

Complexity

  • Time: O(n²)
  • Space: O(1)

2. Iteration — I

Intuition

Only one pass through the array is needed. Maintain a running count of consecutive 1s. When we see a 1, increment the count. When we see a 0, compare the current count with the maximum, then reset to 0. After the loop, do one final comparison — the longest sequence might end at the last element.

Algorithm

  1. Initialize res = 0 and cnt = 0.
  2. Iterate through each element in the array:
    • If the element is 0: update res = max(res, cnt), then reset cnt = 0.
    • If the element is 1: increment cnt.
  3. Return max(cnt, res) — handles sequences that end at the last element.

Solution

def findMaxConsecutiveOnes(nums):
    res = cnt = 0
    for num in nums:
        if num == 0:
            res = max(res, cnt)
            cnt = 0
        else:
            cnt += 1

    return max(cnt, res)


print(findMaxConsecutiveOnes([1, 1, 0, 1, 1, 1]))  # 3
print(findMaxConsecutiveOnes([1, 0, 1, 1, 0, 1]))  # 2

Complexity

  • Time: O(n)
  • Space: O(1)

3. Iteration — II

Intuition

Simplify the logic by updating the maximum on every step. If we see a 1, increment the count; otherwise reset it to 0. Updating res after each element eliminates the need for a final comparison after the loop.

Algorithm

  1. Initialize res = 0 and cnt = 0.
  2. For each element in the array:
    • If the element is 1: increment cnt.
    • Otherwise: set cnt = 0.
    • Update res = max(res, cnt).
  3. Return res.

Solution

def findMaxConsecutiveOnes(nums):
    res = cnt = 0
    for num in nums:
        cnt += 1 if num else -cnt
        res = max(res, cnt)
    return res


print(findMaxConsecutiveOnes([1, 1, 0, 1, 1, 1]))  # 3
print(findMaxConsecutiveOnes([1, 0, 1, 1, 0, 1]))  # 2

Complexity

  • Time: O(n)
  • Space: O(1)

Common Pitfalls

Forgetting the final comparison. When using Iteration — I, the longest sequence might end at the last element. If you only update res when hitting a 0, that final run is never captured. Always compare cnt with res after the loop ends — or use Iteration — II which updates res on every step.

Resetting the counter to 1 instead of 0. When a 0 is encountered, cnt must reset to 0. The next 1 will increment it to 1. Resetting to 1 skips that increment and causes an off-by-one error.