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
Time Complexity: O(n)
Space Complexity: O(1)
View Max Consecutive Bit Visualization