01
Overview

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.

02
Core Concept

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.

03
Key Vocabulary

Vocabulary you'll need.

Used in AP boolean-grid problems.

Vocabulary
  • Boolean grid — a 2D array of true/false values.
  • Per-row count — number of true values 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.
04
AP Exam Focus

How boolean-grid questions are tested.

Initialization values and tie behavior are common traps.

Exam Focus
  • Initialization of bestCount. Use -1 (or Integer.MIN_VALUE) so the first row always wins.
  • Comparison operator. > keeps first match; >= keeps last.
  • Direct boolean as condition. No == true needed; write if (grid[r][c]).
05
Worked Example

Find the most-attended day.

Trace the best-so-far logic with a concrete grid.

Worked Example AP-style reasoning

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 > 2 false → 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.

06
Common Mistakes

Errors that flip the winner.

Small slips invert tie-breaking or skip row 0.

Watch Out
  • Initializing bestCount = 0 when zero is a valid count — the first row may fail to be recorded.
  • Updating bestRow without updating bestCount (or vice versa).
  • Comparing with == and missing actual maxima.
  • Writing if (grid[r][c] == true) — works, but redundant; AP style prefers the direct boolean.
07
Reference Table

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
08
Practice Tip

How to make best-so-far automatic.

A reflex worth building.

Practice Strategy

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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

Topic Quiz 40 min 40 marks Pending

AP CSA Topic Practice: 2D boolean Grid Counting and Best Row

AP-style topic practice assessment

Start
FRQ Practice 30 min 18 marks Pending

AP CSA Unit 05 Topic 02 2D boolean Grid Counting and Best Row FRQ Practice

AP-style topic practice assessment

Start