Max Consecutive Bit Visualization & Animation
Counts the longest run of consecutive 1s in a binary array by resetting a counter on each 0; O(n).
## What is it?
Find the maximum number of consecutive 1s in a binary array. A simple linear scan problem that introduces the concept of tracking a running count of consecutive matches.
## How it works
- Initialize `maxCount = 0`, `currentCount = 0`
- For each bit:
- If `1` → `currentCount++`, update `maxCount = max(maxCount, currentCount)`
- If `0` → reset `currentCount = 0`
- Return `maxCount`
## When to use
- Count consecutive matching elements
- As a building block for "Max Consecutive Ones II/III" (allow flipping k zeros)
- Streak/run-length encoding problems
## Key Points
- O(n) time, O(1) space
- Resetting `currentCount` on a 0 is the key operation
- Extension (Max Consecutive Ones III): use a sliding window tracking at most `k` zeros
Category: algorithms
Difficulty: beginner
- sliding-window
Time Complexity: O(n)
Space Complexity: O(1)
View Max Consecutive Bit VisualizationMax Consecutive Bit
beginnerCounts the longest run of consecutive 1s in a binary array by resetting a counter on each 0; O(n).
streak 0
max 0
New Max!
0
[0]
1
[1]
0
[2]
1
[3]
1
[4]
1
[5]
1
[6]
Current (pointer)
Active streak
Result window
Outside streak
Start scanning. Find the maximum consecutive run of 0s or 1s.
1 / 9