LeetCode Problem Workspace

Max Consecutive Ones

Find the maximum sequence of consecutive 1s in a binary array using an efficient array-driven scanning approach.

category

1

Topics

code_blocks

8

Code langs

hub

3

Related

Practice Focus

Easy · Array-driven solution strategy

bolt

Answer-first summary

Find the maximum sequence of consecutive 1s in a binary array using an efficient array-driven scanning approach.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array-driven solution strategy

Try AiBox Copilotarrow_forward

This problem requires scanning a binary array to identify the longest contiguous sequence of 1s. Start from the beginning, increment a counter for each 1, and reset on 0. Track the maximum value during traversal for the correct result, ensuring an O(n) time array-driven solution.

Problem Statement

You are given a binary array nums consisting of only 0s and 1s. Your task is to determine the length of the longest consecutive sequence of 1s present in the array and return that length.

For example, given nums = [1,1,0,1,1,1], the longest consecutive 1s are three in a row. For nums = [1,0,1,1,0,1], the longest sequence is two. Your solution must handle arrays up to length 10^5 efficiently.

Examples

Example 1

Input: nums = [1,1,0,1,1,1]

Output: 3

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

Example details omitted.

Constraints

  • 1 <= nums.length <= 105
  • nums[i] is either 0 or 1.

Solution Approach

Single-pass Counting

Iterate through the array while maintaining a current count of consecutive 1s. Reset the count to zero whenever a 0 is encountered and continuously update a maximum variable to record the largest sequence seen so far.

Sliding Window Perspective

Consider each segment of consecutive 1s as a window. Track the start and end of this window implicitly while scanning the array, updating the maximum length whenever the window ends due to a 0.

Optimized Space Usage

Only use two integer variables: one for the current streak of 1s and one for the maximum found. This ensures O(1) space while keeping O(n) time, directly reflecting the array-driven solution pattern without extra storage.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity is O(n) because each element is examined exactly once. Space complexity is O(1) since only counters are used, making this approach efficient for large arrays up to length 10^5.

What Interviewers Usually Probe

  • Looking for O(n) time complexity using direct array traversal.
  • Expect awareness of edge cases such as all 1s or all 0s.
  • Interest in minimal extra space and clear variable tracking.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to update the maximum after the last element if the array ends with 1s.
  • Resetting the counter incorrectly or double-counting sequences.
  • Using unnecessary additional arrays instead of simple integer counters.

Follow-up variants

  • Count maximum consecutive zeros instead of ones.
  • Allow flipping at most one 0 to 1 to find the maximum consecutive 1s.
  • Return the start and end indices of the longest consecutive 1s sequence.

FAQ

What is the most efficient way to solve Max Consecutive Ones?

Use a single-pass iteration maintaining a current streak counter and updating a maximum counter, achieving O(n) time and O(1) space.

Can I use a sliding window for Max Consecutive Ones?

Yes, conceptually each consecutive sequence of 1s is a window; track its length while scanning the array.

How do I handle arrays that end with 1?

Ensure the maximum counter is updated after the final element to capture sequences ending at the last index.

What are the common mistakes in this array-driven problem?

Common mistakes include forgetting to reset counters correctly, overusing extra arrays, and missing edge sequences at array ends.

Can this approach handle large arrays up to 10^5 elements?

Yes, the single-pass O(n) time and O(1) space solution scales efficiently to the problem constraints.

terminal

Solution

Solution 1: Single Pass

We can iterate through the array, using a variable $\textit{cnt}$ to record the current number of consecutive 1s, and another variable $\textit{ans}$ to record the maximum number of consecutive 1s.

1
2
3
4
5
6
7
8
9
10
class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        ans = cnt = 0
        for x in nums:
            if x:
                cnt += 1
                ans = max(ans, cnt)
            else:
                cnt = 0
        return ans
Max Consecutive Ones Solution: Array-driven solution strategy | LeetCode #485 Easy