Boolean grids and finding the best row.
When the data is just true/false, the questions simplify — and finding the row with the most "trues" becomes a clean two-loop pattern.
A boolean grid stores only true and false. They model attendance, light/dark, hits/misses — any binary state. Counting trues per row, then finding the maximum, is a classic AP-style task.
This problem combines two patterns: per-row counting and tracking the best so far. The combined template appears throughout AP FRQs.
Count, compare, update best.
Two variables track the winner: bestRow and bestCount.
Boolean grid syntax
boolean[][] attendance = {
{true, true, false},
{true, false, false},
{true, true, true }
};
Counting trues in a row
Boolean cells need no comparison — they are the condition:
if (grid[r][c]) count++;
Finding the row with the most trues
public static int bestRow(boolean[][] grid) {
int bestRow = 0;
int bestCount = -1;
for (int r = 0; r < grid.length; r++) {
int count = 0;
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c]) count++;
}
if (count > bestCount) {
bestCount = count;
bestRow = r;
}
}
return bestRow;
}
Initializing bestCount = -1 ensures row 0 always wins on the first comparison, even if it has zero trues.
Tie-breaking convention
Using > (strict) keeps the earliest row when there's a tie. Using >= keeps the latest. AP problems usually specify which.
Vocabulary you'll need.
Used in AP boolean-grid problems.
- Boolean grid — a 2D array of
true/falsevalues. - Per-row count — number of
truevalues in one row. - Best-so-far — a running record of the maximum.
- Best-row index — the row that currently holds the maximum count.
- Tie-breaking — rule for picking among rows with equal counts.
How boolean-grid questions are tested.
Initialization values and tie behavior are common traps.
- Initialization of
bestCount. Use-1(orInteger.MIN_VALUE) so the first row always wins. - Comparison operator.
>keeps first match;>=keeps last. - Direct boolean as condition. No
== trueneeded; writeif (grid[r][c]).
Find the most-attended day.
Trace the best-so-far logic with a concrete grid.
Problem. Given the attendance grid below, return the row with the highest count.
{{T, T, F},
{T, F, F},
{T, T, T}}
Trace.
- Row 0: count = 2.
2 > -1→ bestRow = 0, bestCount = 2. - Row 1: count = 1.
1 > 2false → no update. - Row 2: count = 3.
3 > 2→ bestRow = 2, bestCount = 3.
Answer. Returns 2.
Why this matters. "Best so far" is the most reusable counting pattern on AP CSA. Master it once and you can apply it to scores, votes, populations, or any other per-row count.
Errors that flip the winner.
Small slips invert tie-breaking or skip row 0.
- Initializing
bestCount = 0when zero is a valid count — the first row may fail to be recorded. - Updating
bestRowwithout updatingbestCount(or vice versa). - Comparing with
==and missing actual maxima. - Writing
if (grid[r][c] == true)— works, but redundant; AP style prefers the direct boolean.
Best-so-far template.
A reusable pattern beyond boolean grids.
Best Row Reference
AP Quick Reference| Variable | Purpose | Initial Value |
|---|---|---|
bestRow |
Index of best row found | 0 |
bestCount |
Best value seen so far | -1 (sentinel) |
count |
Per-row counter | 0 (reset each row) |
> |
Keep earliest tie | AP default |
>= |
Keep latest tie | Use only when specified |
How to make best-so-far automatic.
A reflex worth building.
Use the three practice tools below this lesson in order:
- MCQ Practice — for each, identify the tie-breaking rule first; then trace the running
bestCount. - Java Lab — write best-row, best-column, and best-overall variants. Vary the comparison operator and confirm tie behavior.
- FRQ Practice — when the problem asks for "the row with the most/least", reach for this template immediately.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.