You're given a binary array nums — every element is either 0 or 1. Return the length of the longest unbroken run of 1s in the array.
A run breaks the moment you hit a 0. So you're looking for the widest stretch of consecutive 1s, counting nothing but 1s inside it.
0?1 to a counter on every 1, reset it to 0 on every 0, and keep the largest counter value you ever saw.current and best — are enough. Space is O(1).1s at all, so the longest run has length 0. The counter never leaves 0.nums.length and best ends up equal to it — the run is the entire array.k flips" variant — slide a window that may contain up to k zeros, growing the right edge and shrinking the left whenever the zero-budget is exceeded.O(1)-space single pass already handles a stream perfectly — you never need to hold the whole array in memory.Input: nums = [1,1,0,1,1,1] Output: 3 The first two `1`s form a run of length 2; the last three form a run of length 3. The longest is 3.
Input: nums = [1,0,1,1,0,1] Output: 2 The best run is the middle `1,1`, of length 2.
- 1 <= nums.length <= 10^5 - nums[i] is either 0 or 1.
Max Consecutive Ones is the gentlest introduction to the running-accumulator-with-reset pattern — the same skeleton that powers Kadane's maximum-subarray and every "longest streak" feature you've ever seen. The brute force re-measures a run from every index; the optimal folds all of that into a single sweep.
1s.Given a binary array nums (each element 0 or 1), return the length of the longest unbroken run of 1s. A run ends the moment a 0 appears.
Worked example — nums = [1, 1, 0, 1, 1, 1]
value: 1 1 0 1 1 1 current: 1 2 0 1 2 3 best: 1 2 2 2 2 3 ✓
“Is the array strictly binary — only 0s and 1s?”
Yes here. If other values were allowed, you'd need to define what counts as "in the run".
“Can the array be empty?”
Not in this problem (length >= 1), but if it could, the natural answer is a longest run of 0.
“What if every element is 0?”
There are no 1s, so the answer is 0 — the counter never leaves zero.
“What if every element is 1?”
The run is the whole array, so the answer is the array's length.
“Do I return the length, or the run itself?”
The length. Worth confirming — tracking start/end indices is extra work you may not need.
“How large can the array be?”
Up to 10^5. That matters a lot here: an O(n²) re-scan is ~10^10 operations and will time out, so the single pass isn't just tidier — it's necessary.
Quick checks before I start.
Is the array strictly 0s and 1s?
Do I return the length of the longest run, not the run itself?
What's the maximum length — I want to know if an O(n²) scan is fast enough or if I need O(n)?
Given the size, I'll do a single O(n) pass with a running counter that resets on 0.
The answer is a single number: how long. So you don't need to remember start or end positions, only the length of the run you're currently inside. That collapses a bookkeeping problem into a single counter.
Every 1 extends the current run by one; every 0 ends it, so the counter resets to 0. The answer is simply the largest value that counter ever held — recorded as you go.
The brute force asks "how long is the run starting here?" for all n starting points, re-walking stretches it has already seen (O(n²)). But a single left-to-right sweep already visits every run exactly once — reset-on-zero fuses all those overlapping measurements into one O(n) pass.
| Restart from every index | One pass, reset on zero | |
|---|---|---|
| Idea | Measure the run starting at each i | Count runs as you sweep once |
| Time | O(n²) | O(n) |
| Space | O(1) | O(1) |
| n = 10^5 | ~10^10 ops ✗ | ~10^5 ops ✓ |
The full code for both is in the Approaches selector below.
Key takeaway
Carry a running counter that grows on each 1 and resets on each 0, and track the largest value it ever reaches — one O(n) pass, O(1) memory. This accumulator-with-reset is the same shape as Kadane's algorithm: fold a whole family of overlapping windows into a single sweep by resetting at each boundary.