DEV Community

Jaspreet singh
Jaspreet singh

Posted on

Max Consecutive Ones

Problem Statement

Given a binary array nums, return the maximum number of consecutive 1's present in the array.

Example

Input:

nums = [1,1,0,1,1,1]
Enter fullscreen mode Exit fullscreen mode

Output:

3
Enter fullscreen mode Exit fullscreen mode

Explanation:

The longest sequence of consecutive 1's is [1,1,1]
Length = 3
Enter fullscreen mode Exit fullscreen mode

Brute Force Intuition (Interview Explanation)

For every index, if the element is 1, start moving forward and count how many consecutive 1's exist.

Keep updating the maximum length found so far.

Since for every position we may scan ahead again, many elements get visited multiple times.

Time Complexity

O(N²)
Enter fullscreen mode Exit fullscreen mode

Space Complexity

O(1)
Enter fullscreen mode Exit fullscreen mode

Brute Force Java

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {

        int maxLen = 0;

        for (int i = 0; i < nums.length; i++) {

            int count = 0;

            for (int j = i; j < nums.length; j++) {

                if (nums[j] == 1) {
                    count++;
                    maxLen = Math.max(maxLen, count);
                } else {
                    break;
                }
            }
        }

        return maxLen;
    }
}
Enter fullscreen mode Exit fullscreen mode

Moving Towards Optimal

Notice that we only need the length of the current streak of 1's.

Whenever we encounter:

  • 1 → extend the current streak.
  • 0 → streak breaks, reset count.

Thus, a single traversal is enough.

No extra data structure is required.


Optimal Approach – Running Count

Algorithm

  1. Initialize:
    • curCount = 0
    • maxCount = 0
  2. Traverse the array.
  3. If current element is 1:
    • Increment curCount
    • Update maxCount
  4. If current element is 0:
    • Reset curCount = 0
  5. Return maxCount.

Why It Works

A consecutive sequence continues only while we keep seeing 1's.

Whenever a 0 appears, the sequence breaks and a new count must start after it.

So maintaining only the current streak length is sufficient.


Optimal Java Solution

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {

        int maxCount = 0;
        int curCount = 0;

        for (int num : nums) {

            if (num == 1) {
                curCount++;
                maxCount = Math.max(maxCount, curCount);
            } else {
                curCount = 0;
            }
        }

        return maxCount;
    }
}
Enter fullscreen mode Exit fullscreen mode

Dry Run

Input:

nums = [1,1,0,1,1,1]
Enter fullscreen mode Exit fullscreen mode
Element Current Count Max Count
1 1 1
1 2 2
0 0 2
1 1 2
1 2 2
1 3 3

Final Answer:

3
Enter fullscreen mode Exit fullscreen mode

Pattern Recognition

This pattern appears whenever the problem asks for:

  • Longest consecutive occurrence
  • Longest streak
  • Continuous segment
  • Maximum run length

Common approach:

Maintain Current Streak
Update Global Maximum
Reset on Break Condition
Enter fullscreen mode Exit fullscreen mode

Examples:

  • Max Consecutive Ones
  • Longest Increasing Continuous Segment
  • Longest Repeating Character Block
  • Continuous Attendance/Activity Problems

Interview One-Liner

Since only the current streak of consecutive 1's matters, we maintain a running count, reset it whenever a 0 appears, and continuously update the maximum streak, achieving O(N) time and O(1) space.

Top comments (0)