The O(n) Club: Max Consecutive Ones: Give Your CPU a Break, Not a Headache

The O(n) Club: Max Consecutive Ones — Give Your CPU a Break, Not a Headache

⚡ TL;DR

Want the longest streak of 1s in a binary array? Count as you go, reset when you hit a zero, keep track of your best run, and don’t touch another data structure. Here’s the Java speedrun:

int count = 0, max = 0;
for (int n : nums) {
    count = (n == 1) ? count + 1 : 0;
    max = Math.max(max, count);
}
return max;

🧠 How Devs Usually Mess This Up

Some folks bring a chainsaw to slice bread. Here’s their favorite blunders:

  • Array on array: “What if I copied and transformed?” Congratulations, you used twice the RAM to get the same number.
  • Sliding window syndrome: Cool in advanced problems, totally extra here. There’s nothing to “slide.” It’s just a walk down, not a moonwalk.
  • Forgot to reset: Increment forever, never hit zero, and suddenly your streak is longer than Netflix marathons. Oops!
  • No happy ending: If your max update is hidden inside an else, and the run ends at the last index, enjoy failing that last hidden LeetCode test.

🔍 What’s Actually Going On

This whole task is a relay race. Each 1 passes the baton to the next 1. Hit a 0? You trip, hand your broken baton to a Java variable, and start jogging again. The point is: mark down your record sprint before you wipe the sweat off (count). One pass, two variables: even your ancient laptop can do it while daydreaming.

🛠️ PseudoCode

  • Initialize two counters: max = 0, count = 0
  • Loop through every element in nums:
    • If you see a 1: increment count
    • If you see a 0: reset count
    • Update max to be the bigger of itself or count
  • After you loop, max is the winner—hand it the medal

Simple enough to scribble on a napkin:

int max = 0, count = 0;
for (int n : nums) {
    count = (n == 1) ? count + 1 : 0;
    max = Math.max(max, count);
}
return max;

💻 The Code

public int findMaxConsecutiveOnes(int[] nums) {
    int max = 0;
    int count = 0;
    for (int n : nums) {
        if (n == 1) {
            count++;
            if (count > max) {
                max = count;
            }
        } else {
            count = 0;
        }
    }
    return max;
}

⚠️ Pitfalls, Traps & Runtime Smacks

  • Don’t miss max streaks at the end of the array, or you’ll lose sleep over test cases like [1,1,1].
  • If someone says “What about recursion?” just laugh and move on.
  • All zeros?! Return zero. Don’t try to be clever, just trust the process.
  • All ones?! Return the length of the array. And yes, you still need to write the for-loop.
  • O(n) time, O(1) space—makes your interviewer smile, your code reviewer sigh with relief.

🧠 Interviewer Brain-Tattoo

If you reach for extra arrays, your interviewer quietly schedules another meeting—with someone else. Keep it simple, keep it human.

Previous Article

The Side Effect Club: Airtable: Simplifying Databases for the No-Code Generation

Next Article

The O(n) Club: Running Sum of 1D Array – Because If You Can't Count, You Can't Code