Max Consecutive Ones
Difficulty: Easy Source: NeetCode
Problem
Given a binary array
nums, return the maximum number of consecutive1s in the array.Example 1: Input:
nums = [1, 1, 0, 1, 1, 1]Output:3Example 2: Input:
nums = [1, 0, 1, 1, 0, 1]Output:2Constraints:
1 <= nums.length <= 10^5nums[i]is either0or1
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
- Initialize
res = 0to track the maximum consecutive ones. - For each starting index
i:- Initialize a counter
cnt = 0. - Scan forward from
iwhile the current element is1, incrementingcnt. - Stop when encountering a
0or reaching the end. - Update
res = max(res, cnt).
- Initialize a counter
- 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
- Initialize
res = 0andcnt = 0. - Iterate through each element in the array:
- If the element is
0: updateres = max(res, cnt), then resetcnt = 0. - If the element is
1: incrementcnt.
- If the element is
- 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
- Initialize
res = 0andcnt = 0. - For each element in the array:
- If the element is
1: incrementcnt. - Otherwise: set
cnt = 0. - Update
res = max(res, cnt).
- If the element is
- 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.