01
Overview

Sum once, use twice: the two-pass average.

Some problems can't be solved in a single sweep — the answer needs the average first, then something that compares each element to it.

Accumulation builds a single running total during a traversal. The two-pass average uses that total to compute the mean — and then makes a second pass to compare each element against the mean (count above-average elements, deviations, and so on).

You can't compare to the average inside the first loop because you don't know the average until the loop finishes. That's why the second pass is necessary.

On the AP exam, this pattern appears in stats-style FRQs: "count students whose score is above the class average", or "return the largest deviation from the mean".

02
Core Concept

Pass 1: sum. Compute mean. Pass 2: compare.

The mean is shared across the second pass.

The template

public int countAboveAverage(int[] arr) {
    if (arr.length == 0) return 0;

    int sum = 0;
    for (int n : arr) sum += n;
    double avg = (double) sum / arr.length;

    int count = 0;
    for (int n : arr) {
        if (n > avg) count++;
    }
    return count;
}

Integer division danger

sum / arr.length uses integer division if both operands are int, truncating the decimal. Cast at least one operand to double before dividing.

Empty input

If the array has length 0, the average is undefined. Return early or handle it before the division.

03
Key Vocabulary

Vocabulary you'll need.

Used in any stats-style FRQ.

Vocabulary
  • Accumulator — variable that grows during traversal.
  • Two-pass — two separate traversals over the same array.
  • Average / mean — sum divided by count.
  • Integer division — truncates decimal when both operands are int.
  • Deviation — how far an element is from the mean.
  • Threshold comparison — checking if a value exceeds some computed boundary.
04
AP Exam Focus

How two-pass averages are tested.

Integer division is the most common trap.

Exam Focus
  • Cast for accurate division. (double) sum / arr.length gives the real mean.
  • Empty-array handling. Don't divide by zero.
  • Direction of comparison. "Above" excludes the boundary; "at least" includes it.
  • Two separate loops. Combining them produces wrong logic.
05
Worked Example

Count scores above the class average.

Trace each pass on real numbers.

Worked Example AP-style reasoning

Input: [60, 70, 80, 90, 100].

Pass 1: sum = 400.

Mean: 400 / 5 = 80.0.

Pass 2: values 90 and 100 are above 80.0. count = 2.

Answer: 2 scores above average.

06
Common Mistakes

Pitfalls to avoid.

Most break the mean calculation.

Watch Out
  • Integer division. Without a cast, 7 / 2 is 3, not 3.5.
  • Trying to do both in one pass. You can't compare to the mean before computing it.
  • Empty-array crash. Division by zero throws an exception.
  • Returning the mean instead of the count. Read the prompt's return type.
07
Reference Table

Two-pass at a glance.

Three steps and one cast.

Two-Pass Average Reference

AP Quick Reference
StepActionDetail
1Accumulate sumInitialize to 0; add every element.
2Compute meanCast to double.
3Second passCompare each element to mean.
EdgeEmpty arrayReturn early.
08
Practice Tip

How to master two-pass averages.

Always cast; always handle empty.

Practice Strategy
  • MCQ Practice — verify the cast is present before evaluating the mean.
  • Java Lab — write countAboveAverage, maxDeviation, and percentAboveMean methods. Test with empty arrays.
  • FRQ Practice — for any "compare to average" prompt, write two loops; never one.
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 01 Accumulation and Two-Pass Average FRQ Practice

AP-style topic practice assessment

Start