01
Overview

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.

02
Core Concept

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.

03
Key Vocabulary

Run vocabulary.

Streak-based traversal terminology.

Vocabulary
  • 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.
04
AP Exam Focus

How runs are tested.

Initialization and reset values matter.

Exam Focus
  • 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 best inside the match branch. The reset branch can never beat the best.
05
Worked Example

Find the longest equal run.

Trace both counters every step.

Worked Example AP-style reasoning

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.

06
Common Mistakes

Run-detection pitfalls.

Most are reset or init errors.

Watch Out
  • 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.
07
Reference Table

Run detection at a glance.

Equal-run vs condition-run.

Run Reference

AP Quick Reference
TypeInitReset
Equal-run11
Condition-run00
Loop start1 (equal) or 0 (condition)
Update bestInside match branch
08
Practice Tip

How to master runs.

Trace both counters; memorize init.

Practice Strategy
  • MCQ Practice — write run and best in 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 07 Topic 06 Consecutive Runs FRQ Practice

AP-style topic practice assessment

Start