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".
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.
Vocabulary you'll need.
Used in any stats-style FRQ.
- 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.
How two-pass averages are tested.
Integer division is the most common trap.
- Cast for accurate division.
(double) sum / arr.lengthgives 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.
Count scores above the class average.
Trace each pass on real numbers.
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.
Pitfalls to avoid.
Most break the mean calculation.
- Integer division. Without a cast,
7 / 2is 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.
Two-pass at a glance.
Three steps and one cast.
Two-Pass Average Reference
AP Quick Reference| Step | Action | Detail |
|---|---|---|
| 1 | Accumulate sum | Initialize to 0; add every element. |
| 2 | Compute mean | Cast to double. |
| 3 | Second pass | Compare each element to mean. |
| Edge | Empty array | Return early. |
How to master two-pass averages.
Always cast; always handle empty.
- 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.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 01 Accumulation and Two-Pass Average FRQ Practice
AP-style topic practice assessment