Max Consecutive Ones - Solution & Explanation
Problem Statement
Given a binary array nums, return the maximum number of consecutive 1's in the array.
Example 1:
Input: nums = [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
Example 2:
Input: nums = [1,0,1,1,0,1] Output: 2
Constraints:
1 <= nums.length <= 105nums[i]is either0or1.
Approach Overview
Problem Overview: You receive a binary array containing only 0 and 1. The task is to return the maximum number of consecutive 1s in the array. Each 0 breaks a streak, so you need to track the longest continuous segment of 1s while scanning the array.
Approach 1: Simple Iterative Count (Time: O(n), Space: O(1))
The simplest solution scans the array once while maintaining two counters: currentStreak and maxStreak. When you encounter a 1, increment currentStreak. When a 0 appears, reset currentStreak to zero because the consecutive sequence is broken. After each step, update maxStreak with max(maxStreak, currentStreak). This approach works because every element contributes to exactly one streak calculation. Since the array is processed with a single pass and no extra data structures, the time complexity is O(n) and space complexity is O(1). This pattern appears frequently in array traversal problems where you track a running segment length.
Approach 2: Sliding Window Technique (Time: O(n), Space: O(1))
You can also view the problem through the lens of a sliding window. Maintain two pointers representing the current window of consecutive 1s. Expand the right pointer while elements are 1. When a 0 appears, move the left pointer to the position after the zero and restart the window. At each step, compute the window length right - left + 1 and track the maximum. Although this produces the same complexity as the iterative counter, the sliding window interpretation becomes useful when the problem evolves—for example, allowing one or more zero flips (as in Max Consecutive Ones II and III). The algorithm still processes each element once, giving O(n) time and O(1) space.
Recommended for interviews: The iterative counting approach is what most interviewers expect because it demonstrates clean linear traversal and state tracking. It solves the problem with minimal logic and constant memory. The sliding window version shows deeper pattern recognition and prepares you for harder variants where the window must tolerate a limited number of zeros. Mentioning both approaches signals strong understanding of common array patterns and how they generalize to window-based problems.
Approach 1: Simple Iterative Count
This approach involves iterating through the array and counting sequences of 1s. If a 0 is encountered, the count is reset to 0. We keep track of the maximum count during the iteration.
The function findMaxConsecutiveOnes iterates over the input array nums. It uses a counter to track consecutive ones. When a one is encountered, the counter is incremented. If a zero is encountered, the counter is reset. Throughout the iteration, the maximum sequence length is updated.
Complexity
Time Complexity: O(n), where n is the number of elements in the array, as we make a single pass.
Space Complexity: O(1) since no extra space proportional to input size is used.
Approach 2: Sliding Window Technique
This approach uses a variation of the sliding window technique. The idea is to maintain a window of the current sequence of 1s and adjust the window whenever a 0 is encountered.
In this version, two pointers left and right maintain the window of consecutive 1s. When a 0 is found, left is moved to right + 1, effectively 'skipping' the segments with 0s, adjusting the window accordingly.
Complexity
Time Complexity: O(n)
Space Complexity: O(1)
Approach 3: Single Pass
We can iterate through the array, using a variable cnt to record the current number of consecutive 1s, and another variable ans to record the maximum number of consecutive 1s.
When we encounter a 1, we increment cnt by one, and then update ans to be the maximum of cnt and ans itself, i.e., ans = max(ans, cnt). Otherwise, we reset cnt to 0.
After the iteration ends, we return the value of ans.
The time complexity is O(n), where n is the length of the array. The space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Rust
JavaScript
PHP
Complexity Comparison
| Approach | Complexity |
|---|---|
| Simple Iterative Count | Time Complexity: O(n), where n is the number of elements in the array, as we make a single pass. |
| Sliding Window Technique | Time Complexity: O(n) |
| Single Pass | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simple Iterative Count | O(n) | O(1) | Best general solution. Minimal logic and ideal for interviews. |
| Sliding Window Technique | O(n) | O(1) | Useful when extending to variants allowing zero flips or variable window constraints. |
Video Solution
LeetCode Max Consecutive Ones Solution Explained - Java • Nick White • 18,695 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Max Consecutive Ones easy or hard?
How to solve Max Consecutive Ones in O(n)?
Max Consecutive Ones Python or Java solution?
What is the best approach for Max Consecutive Ones?
What data structure is used in Max Consecutive Ones?
What is the time complexity of Max Consecutive Ones?
Is Max Consecutive Ones asked at Google, Amazon, or Meta?
Ready to solve this problem?
Practice Max Consecutive Ones with our built-in code editor and test cases.
Practice on FleetCode