Skip to main content

Max Consecutive Ones - Solution & Explanation

EasyArray15 min readAsked at: Amazon, Microsoft, Apple +11
Practice this problem

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 <= 105
  • nums[i] is either 0 or 1.

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.

Code

C

C++

Java

Python

C#

JavaScript

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.

Try this approach in the editor →

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(1)

Try this approach in the editor →

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

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Simple Iterative Count

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.

Sliding Window Technique

Time Complexity: O(n)
Space Complexity: O(1)

Single Pass—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simple Iterative CountO(n)O(1)Best general solution. Minimal logic and ideal for interviews.
Sliding Window TechniqueO(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?
Max Consecutive Ones is classified as an Easy problem on LeetCode with an acceptance rate above 60%. The challenge focuses on clean iteration and tracking a running segment length rather than complex algorithms.
How to solve Max Consecutive Ones in O(n)?
Iterate through the binary array while maintaining two variables: current streak and maximum streak. Increment the current streak when the value is 1, otherwise reset it to zero. Update the maximum after each step. This guarantees O(n) time and constant memory usage.
Max Consecutive Ones Python or Java solution?
Python and Java solutions both follow the same logic: iterate through the array, update a running count of consecutive ones, and reset on zeros. The implementation typically fits in a few lines and runs in O(n) time with O(1) space.
What is the best approach for Max Consecutive Ones?
The optimal approach is a single-pass iterative counter. Traverse the array, increase a running counter when you see a 1, and reset it when a 0 appears. Track the maximum streak during the traversal. This runs in O(n) time and O(1) space.
What data structure is used in Max Consecutive Ones?
The problem primarily uses a simple array traversal with integer counters. No additional data structures are required. Some explanations frame the logic using the sliding window technique, but it still operates directly on the array with constant extra space.
What is the time complexity of Max Consecutive Ones?
The standard solution runs in O(n) time because the array is scanned once from left to right. Each element is processed exactly one time. Space complexity remains O(1) since only a few integer counters are maintained.
Is Max Consecutive Ones asked at Google, Amazon, or Meta?
Max Consecutive Ones is a common entry-level array problem and has appeared in interview prep lists associated with companies like Amazon and Google. It is often used to test basic array traversal, state tracking, and understanding of sliding window patterns.

Ready to solve this problem?

Practice Max Consecutive Ones with our built-in code editor and test cases.

Practice on FleetCode