Average and minimum — but only over qualifying elements.
When a stat applies only to elements that meet a condition, count only what counts.
Conditional average means averaging only elements that satisfy some test. Conditional minimum finds the smallest among those that qualify. Both patterns need careful handling of the case where no element qualifies — division by zero or a meaningless sentinel return.
The trick is a filtered accumulator: a sum and a counter that both grow only when the condition holds.
On the AP exam, this appears in FRQs like "average grade of students who passed" or "minimum price among items in stock".
Sum and count only the qualifying elements.
Guard the divide; guard the minimum.
Conditional average
public double averagePositive(int[] arr) {
int sum = 0;
int count = 0;
for (int n : arr) {
if (n > 0) {
sum += n;
count++;
}
}
if (count == 0) return 0.0; // or per spec
return (double) sum / count;
}
Conditional minimum
public int minPositive(int[] arr) {
int min = Integer.MAX_VALUE;
boolean found = false;
for (int n : arr) {
if (n > 0 && n < min) {
min = n;
found = true;
}
}
return found ? min : -1;
}
The found flag signals whether any element qualified — essential when the array might have no positives.
Conditional-stat vocabulary.
Used in filtered-aggregate problems.
- Conditional accumulator — sum that grows only when the condition holds.
- Conditional count — counter that grows only when the condition holds.
- Empty-subset case — no qualifying elements.
- Found flag — boolean tracking whether any qualifier was seen.
- Sentinel return — value used when no qualifier exists.
How conditional stats are tested.
Empty-subset handling and dividing by the right count dominate.
- Divide by the conditional count. Not
arr.length. - Guard the divide. Return a sensible value if count is 0.
- Cast for accurate division. Avoid integer truncation.
- Found flag for minimum. Avoids returning
Integer.MAX_VALUEas the answer.
Average and min of positives.
Trace both methods on the same input.
Input: [-3, 5, -1, 8, 2].
Average of positives: sum = 15, count = 3 → 15/3 = 5.0.
Min of positives: 2.
Edge case. If input were [-3, -1, -7], the average method returns 0.0 (or per spec) and the min method returns -1.
Conditional-stat pitfalls.
Most come from wrong divisor or missing guard.
- Dividing by
arr.lengthinstead of conditional count. Skews the average. - No empty-subset guard. Division by zero crash.
- Returning
Integer.MAX_VALUE. Better to return -1 or per spec. - Combining the conditions awkwardly. Apply both: condition AND new min.
Conditional accumulators.
Sum, count, min — guarded by the same predicate.
Conditional Stats Reference
AP Quick Reference| Stat | Initialize | Empty case |
|---|---|---|
| Conditional sum | 0 | Returns 0. |
| Conditional count | 0 | Returns 0. |
| Conditional average | sum/count | Guard divisor. |
| Conditional min | Integer.MAX_VALUE | Found flag. |
How to master conditional stats.
Always test the all-fail case.
- MCQ Practice — verify the divisor matches the conditional count, not the array length.
- Java Lab — write averagePositive, minPositive, averagePassing. Test with all-failing inputs.
- FRQ Practice — guard division and minimum-find against the empty case explicitly.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 05 Conditional Average and Minimum FRQ Practice
AP-style topic practice assessment