LearnWithKiriti

Max Consecutive Ones

đź’» Try it yourself first. Write your solution in Java, C++, Python or JavaScript and run it against hidden tests, right here. New: mock interview with Mr. Tock. Explain your approach out loud, he pushes you to improve it, then you code it. You get a scorecard at the end.
Play
On this page
  1. Revision notes
  2. Brute
  3. Optimal

Revision notes

Max Consecutive Ones

Video: “Max Consecutive Ones: why your code returns 2, not 3” on LearnWithKiriti (Kodee). LeetCode 485.

Return the length of the longest run of 1s in an array of 0s and 1s. Example: [1, 1, 0, 1, 1, 1] -> 3

Folder Approach Time Extra space
Brute/ Brute force: From every start, count the 1s that follow, and keep the biggest count. The same 1s get counted again and again. O(n²) worst (all 1s) O(1)
Optimal/ Optimal: One pass: current counts the run you are in, a 0 resets it, and maxCount is updated inside the 1 branch, so a run that ends the array is not lost. O(n) O(1)

Rebuild it

Count the run you are in; a 0 resets it. The classic bug updates maxCount only when a 0 arrives, so a run that ends the array (the 3 here) is never saved: it returns 2. Update maxCount inside the 1 branch instead.

See each folder’s pseudocode.md. Every program runs Example 1 and prints the answer; the same code is in the practice panel’s Start menu on learnwithkiriti.com, where Shelf Bot can act it out.

brute

Brute

Brute: count from every start

From every start, count the 1s that follow, and keep the biggest count. The same 1s get counted again and again.

maxCount = 0
for start from 0 to size - 1:
    count = 0
    scan = start
    while scan < size and nums[scan] == 1:
        count = count + 1
        scan = scan + 1
    maxCount = max(maxCount, count)
return maxCount

O(n²) when the array is all 1s: every start counts the same 1s again.

MaxConsecutiveOnesBrute.java
import java.util.*;

public class MaxConsecutiveOnesBrute {
    public int findMaxConsecutiveOnes(int[] nums) {
        int maxCount = 0;
        for (int start = 0; start < nums.length; start++) {
            int count = 0;
            int scan = start;
            while (scan < nums.length && nums[scan] == 1) {
                count++;
                scan++;
            }
            maxCount = Math.max(maxCount, count);
        }
        return maxCount;
    }

    public static void main(String[] args) {
        MaxConsecutiveOnesBrute solver = new MaxConsecutiveOnesBrute();
        int[] nums = new int[]{1, 1, 0, 1, 1, 1};
        int answer = solver.findMaxConsecutiveOnes(nums);
        System.out.println(answer);
    }
}
optimal

Optimal

Optimal: one pass

One pass: current counts the run you are in, a 0 resets it, and maxCount is updated inside the 1 branch, so a run that ends the array is not lost.

maxCount = 0
current = 0
for each light in nums:
    if light == 1:
        current = current + 1
        if current > maxCount: maxCount = current
    else:
        current = 0
return maxCount

Dry run on Example 1:

light current maxCount
1 1 1
1 2 2
0 0 2
1 1 2
1 2 2
1 3 3

O(n) time, O(1) extra space.

MaxConsecutiveOnesOptimal.java
import java.util.*;

public class MaxConsecutiveOnesOptimal {
    public int findMaxConsecutiveOnes(int[] nums) {
        int maxCount = 0;
        int current = 0;
        for (int light : nums) {
            if (light == 1) {
                current++;
                if (current > maxCount) {
                    maxCount = current;
                }
            } else {
                current = 0;
            }
        }
        return maxCount;
    }

    public static void main(String[] args) {
        MaxConsecutiveOnesOptimal solver = new MaxConsecutiveOnesOptimal();
        int[] nums = new int[]{1, 1, 0, 1, 1, 1};
        int answer = solver.findMaxConsecutiveOnes(nums);
        System.out.println(answer);
    }
}