Detecting consecutive runs in a 1D array.
Find the longest streak of equal (or condition-matching) adjacent elements.
A run is a maximal sequence of consecutive elements that share a property — equal values, all positive, all even. Run-detection problems track a current streak and a best-so-far maximum.
Two counters do all the work: run grows when the condition continues; best records the longest run seen.
On the AP exam, run problems test loop discipline — reset on break, update best inside the match branch, start counter at 1.
Two counters: current run, best so far.
Grow on match; reset to 1 on break.
Longest equal-run
public int longestEqualRun(int[] arr) {
if (arr.length == 0) return 0;
int best = 1;
int run = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == arr[i - 1]) {
run++;
if (run > best) best = run;
} else {
run = 1;
}
}
return best;
}
Run by condition (e.g., positive)
int run = 0, best = 0;
for (int n : arr) {
if (n > 0) {
run++;
if (run > best) best = run;
} else {
run = 0;
}
}
Different reset value: 0 because the condition can fail at the very first element.
Run vocabulary.
Streak-based traversal terminology.
- Run — maximal sequence of matching adjacent elements.
- Streak counter — variable tracking the current run.
- Best-so-far — running maximum of all observed runs.
- Reset — return streak to start value when break occurs.
- Match condition — predicate that defines what counts as a run.
How runs are tested.
Initialization and reset values matter.
- Equal-run starts at 1. A single cell is a run of 1.
- Condition-run starts at 0. Resets to 0 on failure.
- Loop starts at 1 for equal-run (compares to previous).
- Update
bestinside the match branch. The reset branch can never beat the best.
Find the longest equal run.
Trace both counters every step.
Input: [2, 2, 5, 5, 5, 1].
Trace. run=1, best=1.
- i=1: 2==2 → run=2, best=2.
- i=2: 5!=2 → run=1.
- i=3: 5==5 → run=2.
- i=4: 5==5 → run=3, best=3.
- i=5: 1!=5 → run=1.
Answer: 3.
Run-detection pitfalls.
Most are reset or init errors.
- Init run/best to 0 for equal-run. Off-by-one — a single element should report 1.
- Reset run to 0 in equal-run. The breaking cell itself starts a new run of 1.
- Loop start at 0 with
arr[i - 1]. Out-of-bounds. - Updating best in the reset branch. Wastes cycles; can't be the best.
Run detection at a glance.
Equal-run vs condition-run.
Run Reference
AP Quick Reference| Type | Init | Reset |
|---|---|---|
| Equal-run | 1 | 1 |
| Condition-run | 0 | 0 |
| Loop start | 1 (equal) or 0 (condition) | — |
| Update best | Inside match branch | — |
How to master runs.
Trace both counters; memorize init.
- MCQ Practice — write
runandbestin a table for each cell. - Java Lab — implement longestEqualRun, longestPositiveRun, longestEvenRun. Test on monotone and constant arrays.
- FRQ Practice — start with the two-counter template; fill in match condition last.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 06 Consecutive Runs FRQ Practice
AP-style topic practice assessment