Three classics in one traversal.
Sum every element, count those matching a condition, and locate the index of the minimum — all in a single pass.
The three most common array-traversal computations — total sum, conditional count, and argmin — can run side by side in one loop. Each uses its own accumulator; all share the same iteration.
Combining them is more efficient than three separate loops and demonstrates clean accumulator discipline.
On the AP exam, these three patterns appear individually and in combination — and recognizing all three quickly is a sign of array fluency.
Three accumulators, one loop.
Sum grows, count grows conditionally, argmin updates on improvement.
Combined template
int sum = 0;
int count = 0;
int minIndex = 0;
int minValue = arr[0];
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
if (arr[i] > threshold) count++;
if (arr[i] < minValue) {
minValue = arr[i];
minIndex = i;
}
}
Initialization checklist
- Sum: 0.
- Count: 0.
- minValue:
arr[0](not 0 orInteger.MAX_VALUEfor clarity). - minIndex: 0.
Each pattern alone
// Sum only
int sum = 0;
for (int n : arr) sum += n;
// Conditional count only
int count = 0;
for (int n : arr) if (n > threshold) count++;
// Argmin only
int minIndex = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] < arr[minIndex]) minIndex = i;
}
Combined-accumulator vocabulary.
Three patterns; one loop.
- Sum — total of all elements.
- Conditional count — number of elements matching a predicate.
- Argmin — index of the minimum value.
- Accumulator — variable updated each iteration.
- Combined pass — multiple computations in one loop.
- Initialization — starting value before the loop.
How combined patterns are tested.
Initialization and return type confusion dominate.
- Init minValue to arr[0]. Not 0 — fails on all-negative arrays.
- Return index for argmin. Not the value.
- Condition direction. Read prompt carefully ("above" vs "at least").
- One pass is fine. Separate loops are not penalized but unnecessary.
All three in one pass.
Trace each accumulator.
Input: [7, 3, 9, 1, 5], threshold = 4.
Trace.
- i=0: sum=7; count=1 (7>4); minIndex=0, minValue=7.
- i=1: sum=10; count=1; 3<7 → minIndex=1, minValue=3.
- i=2: sum=19; count=2; 9 not min.
- i=3: sum=20; count=2; 1<3 → minIndex=3, minValue=1.
- i=4: sum=25; count=3 (5>4); 5 not min.
Results: sum=25, count=3, argmin=3.
Combined-pattern pitfalls.
Most come from initializations.
- minValue = 0. Breaks for all-negative arrays.
- Returning minValue when argmin is asked for. Read the prompt.
- Updating only minIndex without minValue. They must move together.
- Confusing > with >=. Strict vs non-strict changes tie behavior.
Three-pattern initializations.
Side by side.
Combined Accumulator Reference
AP Quick Reference| Pattern | Initialize | Update |
|---|---|---|
| Sum | 0 | sum += arr[i] |
| Conditional count | 0 | if (cond) count++ |
| Argmin index | 0 | Update when smaller. |
| Argmin value | arr[0] | Update alongside index. |
How to master combined accumulators.
Three accumulators in one loop — drill until automatic.
- MCQ Practice — verify each accumulator updates with the right operator and inside the right branch.
- Java Lab — implement combinedStats returning all three values. Test with negatives and ties.
- FRQ Practice — when an FRQ wants several aggregates, combine into one pass for clean code.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 15 Sum, Conditional Count, and Argmin FRQ Practice
AP-style topic practice assessment