Max Consecutive Ones: Single Pass Counter
LeetCode Max Consecutive Ones (problem 485) gives you a binary array nums containing only 0s and 1s. Your job is to find the maximum number of consecutive 1s in the array.
Given nums = [1, 1, 0, 1, 1, 1], the answer is 3. The longest run of 1s spans indices 3, 4, and 5. There is also a shorter run of two 1s at the start, but the one at the end wins.
Why this problem matters
Max Consecutive Ones is one of the cleanest introductions to the "running counter" pattern. You scan the array once, maintain a counter for the current streak, and track the best streak you have seen so far. That same skeleton appears whenever you need to find the longest run of anything: consecutive characters in a string, increasing elements in a sequence, or valid subarrays matching some condition.
The problem is rated easy, and the solution is short. But the underlying idea of maintaining a running count alongside a global max is a building block that you will use in dozens of harder problems.
The approach: single pass with two variables
You only need two variables:
counttracks how many consecutive 1s you have seen in the current streak.max_counttracks the longest streak found so far.
Walk through the array from left to right. When you see a 1, increment count and update max_count if count exceeds it. When you see a 0, reset count to zero. The streak is broken.
By the time you reach the end, max_count holds the answer.
Python solution
def findMaxConsecutiveOnes(nums):
count = 0
max_count = 0
for num in nums:
if num == 1:
count += 1
if count > max_count:
max_count = count
else:
count = 0
return max_count
Step-by-step walkthrough
Trace through nums = [1, 1, 0, 1, 1, 1]. Watch how count grows during a streak of 1s and resets to zero whenever a 0 appears. Meanwhile, max_count only increases when the current streak beats the previous best.
Start: Initialize count = 0 and max_count = 0.
Both counters begin at zero. We have not scanned any element yet.
Index 0: nums[0] = 1. Increment count.
count becomes 1. It is greater than max_count (0), so update max_count to 1.
Index 1: nums[1] = 1. Increment count.
count becomes 2. It is greater than max_count (1), so update max_count to 2.
Index 2: nums[2] = 0. Reset count to 0.
The streak breaks. count resets to 0. max_count stays at 2.
Index 3: nums[3] = 1. Increment count.
A new streak begins. count becomes 1. max_count stays at 2.
Index 4: nums[4] = 1. Increment count.
count becomes 2. It ties max_count but does not exceed it. max_count stays at 2.
Index 5: nums[5] = 1. Increment count.
count becomes 3. It exceeds max_count (2), so update max_count to 3.
Done: End of array. Return max_count = 3.
The longest consecutive sequence of 1s in [1, 1, 0, 1, 1, 1] has length 3.
The key moment is index 5. That is where count reaches 3 and overtakes the previous max_count of 2. Everything before that was building up to this comparison.
Complexity analysis
| Metric | Value |
|---|---|
| Time | O(n), single pass through the array |
| Space | O(1), only two integer variables |
Every element is visited exactly once. No extra data structures, no nested loops. This is as efficient as it gets for this problem.
The building blocks
Running counter with reset
The core mechanic is a counter that increments while a condition holds and resets when it breaks. This pattern generalizes to any "longest run" question:
count = 0
max_count = 0
for item in sequence:
if condition(item):
count += 1
max_count = max(max_count, count)
else:
count = 0
Swap in your own condition and the structure stays identical. You will see this in problems like Max Consecutive Ones II (with at most one flip allowed) and Longest Turbulent Subarray.
Max tracking
Keeping a separate max_count variable that updates only when the current value exceeds it is a micro-pattern that appears everywhere. Kadane's algorithm for Maximum Subarray uses the same idea: maintain a running value and a global best, updating the global best whenever the running value improves.
Edge cases
- All ones:
nums = [1, 1, 1, 1]. The counter never resets.max_countequalslen(nums). Return 4. - All zeros:
nums = [0, 0, 0]. The counter never increments.max_countstays at 0. Return 0. - Single element:
nums = [1]returns 1.nums = [0]returns 0. The loop runs once and the answer is immediate. - Alternating:
nums = [1, 0, 1, 0, 1]. The counter resets at every 0. Each streak has length 1. Return 1.
From understanding to recall
This problem feels almost too simple when you read the walkthrough. The danger is skipping practice because it seems obvious. But in an interview setting, even easy problems can trip you up if you have not practiced writing them from scratch. Do you initialize count and max_count both to zero or to something else? Do you update max_count inside the if block or after the loop?
Write the solution on a blank screen. Trace through a short array by hand. Do it again in a few days. The goal is not to memorize the code but to make the running-counter pattern feel automatic so you can apply it without hesitation when the problem is harder.
Related posts
- Move Zeroes - Another single-pass array problem that uses a different pointer technique to rearrange elements in-place.
- Find All Numbers Disappeared in an Array - Uses the array itself to track which values are present, a different take on O(1) space array scanning.
- Remove Duplicates from Sorted Array - A read-write pointer approach that also processes an array in a single pass with constant space.