Watch 10 video solutions for Max Consecutive Ones, a easy level problem involving Array. This walkthrough by Nick White has 18,579 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 either 0 or 1.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 | 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. |