Last Updated: March 31, 2026
We have an array that only contains 0's and 1's, and we need to find the longest streak of consecutive 1's. Think of it like counting the longest winning streak in a series of games where 1 means a win and 0 means a loss.
The key observation is simple: as we scan through the array, we maintain a running count of consecutive 1's. Every time we hit a 0, the streak breaks and we reset the count. At each step, we keep track of the maximum streak we've seen so far. That's really all there is to it. The challenge isn't finding a clever algorithm, it's implementing the straightforward one cleanly without off-by-one errors.
1 <= nums.length <= 10^5 → With n up to 100,000, we need O(n) or O(n log n). An O(n^2) approach (checking every subarray) would be 10 billion operations, which is too slow.nums[i] is either 0 or 1 → This is a binary array. We don't need to worry about negative numbers, large values, or any other edge cases related to element values. The only two states are "part of a streak" or "streak breaker."The most straightforward approach: for every position in the array, check how long the streak of consecutive 1's starting at that position is. Then take the maximum across all starting positions.
This is what you'd do if you were scanning through the array by hand and being extra careful. Pick a starting index, count how many 1's follow in a row, write that down, move to the next starting index, and repeat.
maxCount = 0 to track the best streak found.i from 0 to n - 1:nums[i] is 1, start counting consecutive 1's from index i.i, incrementing a counter while elements are 1.maxCount with the streak length if it's larger.maxCount.This brute force recounts the same 1's from every starting position in a streak. What if we just kept a running count that grows with each 1 and resets at each 0?
Here's the key insight: we don't need to restart counting from each position. We can just walk through the array once, keeping a running counter. When we see a 1, we increment the counter. When we see a 0, the streak is broken, so we reset the counter to 0. At every step, we check if the current streak is the longest we've seen.
The running counter maintains a simple invariant: count always holds the length of the current streak of 1's ending at the current position. If we're on a 1, the streak extends by one. If we're on a 0, no streak ends here, so the count goes to 0. By checking maxCount after every element, we guarantee we capture the longest streak even if it occurs in the middle of the array and gets broken by a later 0.
This is essentially a simplified version of Kadane's algorithm. Kadane's tracks the "best subarray sum ending here" for the maximum subarray problem. Here, we track the "longest run of 1's ending here." The pattern is the same: maintain a local running value, reset when appropriate, and track the global best.
maxCount = 0 and count = 0.nums.1, increment count.0, reset count to 0.maxCount = max(maxCount, count).maxCount.count and maxCount). No extra data structures regardless of input size.