Given an array arr[] consisting of only 0’s and 1’s, return count of the…
20262025
Given an array arr[] consisting of only 0’s and 1’s, return count of the maximum number of consecutive 1’s or 0’s present in the array.
Input format
The first line contains a single integer n — the number of elements in arr[].
The second line contains n space-separated integers arr[0], arr[1], …, arr[n-1], each of which is 0 or 1.
Output format
Print a single integer — the maximum number of consecutive equal values (1’s or 0’s) present in the array.
Examples:
Input:
7
0 1 0 1 1 1 1
Output: 4
Explanation: The maximum number of consecutive 1’s in the array is 4 from index 3-6.
Input:
6
0 0 1 0 1 0
Output: 2
Explanation: The maximum number of consecutive 0’s in the array is 2 from index 0-1.
Input:
4
0 0 0 0
Output: 4
Explanation: The maximum number of consecutive 0’s in the array is 4.
Constraints:
1 ≤ n ≤ 105
0 ≤ arr[i] ≤ 1
Attempted by 4 students.
Show answer & explanation
Concept — the running-run scan.
A run is a maximal block of positions holding the same value. For any sequence, the length of the run that ends at position i depends only on position i-1: if the two values are equal the run continues, otherwise a fresh run of length 1 begins there. So one left-to-right pass that carries two numbers — cur (length of the run ending here) and best (longest run seen so far) — is enough to find the longest run in the whole sequence. The rule never inspects which value is being repeated, so the same single scan answers "longest block of 1s" and "longest block of 0s" together; no second pass and no separate counter per value is needed.
The invariant, stated once:
Initialise
cur = 1andbest = 1at index 0 — a one-element sequence already contains a run of length 1, and the constraints guarantee at least one element.For each index i from 1 to n-1: if
arr[i] == arr[i-1]thencur = cur + 1; otherwisecur = 1.Immediately after updating, set
best = max(best, cur)so the final run is counted like every other run.After the loop,
bestis the length of the longest block of equal values.
Application — tracing the first example.
For arr[] = [0, 1, 0, 1, 1, 1, 1] the two carried numbers evolve as follows:
i | arr[i] | equal to arr[i-1]? | cur | best |
|---|---|---|---|---|
0 | 0 | start | 1 | 1 |
1 | 1 | no | 1 | 1 |
2 | 0 | no | 1 | 1 |
3 | 1 | no | 1 | 1 |
4 | 1 | yes | 2 | 2 |
5 | 1 | yes | 3 | 3 |
6 | 1 | yes | 4 | 4 |
The scan ends with best = 4, produced by the block of 1s spanning indices 3 to 6.
Cross-check — the other two examples and the boundary.
arr[] = [0, 0, 1, 0, 1, 0]: the successive cur values are 1, 2, 1, 1, 1, 1, so best = 2 — the block of 0s at indices 0 and 1. Note that the longest block here is made of 0s, which is why the scan must not be restricted to 1s.
arr[] = [0, 0, 0, 0]: cur climbs 1, 2, 3, 4 and best follows to 4. Because best is updated inside the loop rather than only when a run breaks, the run that reaches the end of the array is still counted.
n = 1: the loop body never executes and best stays at its initial 1, which is the correct answer for a single element.
Reference approach in pseudocode.
read n
read arr[0..n-1]
cur = 1
best = 1
for i = 1 to n-1:
if arr[i] == arr[i-1]:
cur = cur + 1
else:
cur = 1
if cur > best:
best = cur
print bestComplexity and common mistakes.
Time O(n), extra space O(1): every index is visited once and only two integers are carried. With n up to 105, a single pass finishes far inside the 2000 ms limit, and reading the input with a buffered reader rather than a per-token scanner is the practical bottleneck in Java and Python.
Resetting
curto 0 instead of 1 when the value changes undercounts every run by one, because the element at the break is itself the first element of the new run.Updating
bestonly at the moment a run breaks loses the final run entirely, which is exactly the [0, 0, 0, 0] case.Counting only 1s and ignoring 0s answers a different question and fails on [0, 0, 1, 0, 1, 0].