This problem is a classic in coding interviews at companies like Google, Amazon, and Microsoft because it tests a candidate’s ability to track state efficiently while traversing an array.
Problem statement
You are given a binary array nums containing only 0s and 1s. Your task is to find and return the maximum number of consecutive 1s present in the array.
Constraints:
- 1 <=
nums.length<= 105 nums[i]is either 0 or 1.
Examples
| Input | Output | Explanation |
[1, 1, 1, 0, 0, 1, 1] | 3 | The first three elements form the longest consecutive sequence of 1’s. |
[0, 1, 1, 1, 1, 0, 1] | 4 | Elements at indexes 1-4 form the longest consecutive sequence of 1’s. |
[1, 0, 1, 0, 1, 0, 1] | 1 | Each 1 appears individually, so the maximum consecutive count is 1. |
[0, 0, 0, 0, 0] | 0 | The array doesn’t contain any 1’s, so the answer is 0. |
[1, 1, 1, 1, 1] | 5 | All elements are 1’s, so the entire array length is the answer. |
Optimized approach using single-pass traversal
We can solve it by maintaining just two variables as we iterate through the array once: the current consecutive count of 1s, and the longest consecutive count we’ve encountered so far.
This approach is effective because the problem’s structure inherently defines clear boundaries between consecutive sequences. When we encounter a 1, we’re either continuing an existing streak or starting a new one, so we increment our current count. When we hit a 0, we know the streak has been broken, so we reset our current count to zero. Throughout this process, we continuously update our maximum to capture the longest streak encountered so far.
Step-by-step algorithm
- Initialize
countto0to track the current streak of consecutive 1s - Initialize
max_countto0to store the longest streak found so far - Iterate through each element
numin the arraynums:- If
numequals1:- Increment
countto extend the current streak - Update
max_countto be the maximum ofmax_countandcount.
- Increment
- Otherwise,
- Reset
countto0because the streak has been interrupted
- Reset
- If
- Return
max_countas the final result
Let’s trace through the algorithm using the following illustration:
Code implementation
Let’s look at the following code to understand its implementation.
Code for the max consecutive ones problem
Time complexity
The time complexity of this algorithm is O(n), where n is the length of the input array nums. We traverse the array exactly once, performing constant-time operations O(1) for each element.
Space complexity
The space complexity is O(1) because we use only a fixed amount of extra memory regardless of the input size.
Common pitfalls and interview tips
- Forgetting to reset the counter: A common mistake is failing to reset
countto 0 when encountering a 0. This leads to incorrect results because the algorithm continues counting 1s across different segments. Always ensure that your streak counter resets immediately upon hitting a 0. - Handling edge cases: Be prepared to discuss edge cases like an array of all 0s (should return 0) or all 1s (should return the array length). Also consider the single-element cases:
[1]should return 1, and[0]should return 0. Testing these cases demonstrates thoroughness.