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, we count how many consecutive 1s start from that position. We scan forward until we hit 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 to 0 to track the maximum consecutive ones.
  2. For each starting index i:
    • Initialize a counter cnt to 0.
    • Scan forward from i while the current element is 1, incrementing the counter.
    • Stop when encountering a 0 or reaching the end.
    • Update res with the maximum of res and cnt.
  3. Return res.
class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        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

Time & Space Complexity

  • Time complexity: O(n2)O(n ^ 2)
  • Space complexity: O(1)O(1)

2. Iteration - I

Intuition

We only need one pass through the array. 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 the count to 0. After the loop, we do one final comparison since the longest sequence might end at the last element.

Algorithm

  1. Initialize res and cnt to 0.
  2. Iterate through each element in the array:
    • If the element is 0, update res with the maximum of res and cnt, then reset cnt to 0.
    • If the element is 1, increment cnt.
  3. Return the maximum of res and cnt (to handle sequences ending at the array's end).
class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        res = cnt = 0
        for num in nums:
            if num == 0:
                res = max(res, cnt)
                cnt = 0
            else:
                cnt += 1

        return max(cnt, res)

Time & Space Complexity

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

3. Iteration - II

Intuition

We can simplify the logic by updating the maximum inside the loop at every step. If we see a 1, we increment the count; otherwise, we reset it to 0. After each element, we update the result. This eliminates the need for a final comparison after the loop.

Algorithm

  1. Initialize res and cnt to 0.
  2. For each element in the array:
    • If the element is 1, increment cnt.
    • Otherwise, set cnt to 0.
    • Update res to be the maximum of res and cnt.
  3. Return res.
class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        res = cnt = 0
        for num in nums:
            cnt += 1 if num else -cnt
            res = max(res, cnt)
        return res

Time & Space Complexity

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

Common Pitfalls

Forgetting the Final Comparison

When tracking consecutive ones by resetting the counter on encountering a zero, the longest sequence might end at the last element of the array. If you only update the maximum inside the loop when hitting a zero, you will miss this case. Always compare the final count with the result after the loop ends, or update the maximum on every iteration.

Resetting the Counter Incorrectly

A subtle bug is resetting the counter to 1 instead of 0 when encountering a zero, or incrementing before checking the current element. The counter should be reset to 0 when a zero is found, then the next 1 will increment it to 1. Mixing up this logic leads to off-by-one errors in the count.