Topic 4.13 Notes – Implementing 2D Array Algorithms
1. What 2D Array Algorithms Are
A 2D array looks like a grid or spreadsheet. You access elements with:
arr[row][col]
Two length rules you must know:
arr.length→ number of rowsarr[row].length→ number of columns in that row
Here’s the mental model. This example shows a 4×3 array, so there are 4 rows and 3 columns. The highlighted cell is a[2][1], which means row 2, column 1.

2D array indexed by row and column
Most algorithms use row-major traversal:
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[row].length; col++) {
// process arr[row][col]
}
}
Outer loop controls rows. Inner loop controls columns.
If you understand 1D array patterns, 2D is just nesting that logic.
2. Standard 2D Traversal Algorithms
These are the patterns the AP expects you to recognize and write.
Minimum and Maximum
Same pattern as 1D:
- Initialize to a relevant starting value (often
arr[0][0]). - Traverse required cells.
- Update when a better value is found.
Scopes they love to test:
- Entire array
- A specific row
- A specific column
- A subsection (region)
If you need the location, track both bestRow and bestCol.
A common combo question:
“Which row has the greatest sum?”
That’s sum each row → compare row totals → track index.
Sum and Average
Sum
- Start accumulator at 0.
- Add each relevant element.
Average
average = (double) sum / count- Cast to
doubleto avoid integer division.
Know your counts:
- Entire array → total elements = add them as you traverse (safest method)
- Row →
arr[row].length - Column →
arr.length
If computing row-by-row totals, reset sum = 0 inside the outer loop.
Searching and Boolean Checks
These follow exact 1D logic.
At least one element has property
return true; // immediately when found
Return false after full traversal.
All elements have property
Return false immediately when violation found.
Counting elements
Increment a counter when condition holds.
Early returns are common in AP scoring guidelines. If you find what you’re looking for, stop.
Consecutive Pairs and Duplicates
You’ll see patterns involving neighbors.
Horizontal pairs
for each row
for col < arr[row].length - 1
compare arr[row][col] and arr[row][col+1]
Vertical pairs
for each column
for row < arr.length - 1
compare arr[row][col] and arr[row+1][col]
Notice the - 1. That prevents out-of-bounds errors.
Duplicates often require comparing elements across the array. That may mean nested comparisons or using an ArrayList to track seen values.
Shifting and Reversing
These modify data, so index order matters.
Shift row left (wraparound idea):
- Save first element.
- Move everything left.
- Put saved value at end.
Reverse a row
Use two pointers:
int left = 0;
int right = arr[row].length - 1;
while (left < right) {
int temp = arr[row][left];
arr[row][left] = arr[row][right];
arr[row][right] = temp;
left++;
right--;
}
Columns work the same way but vary the row index instead.
Overwrite mistakes happen when you shift in the wrong direction.
3. Writing Original 2D Algorithms
FRQ 4 usually combines patterns.
Typical structure:
- Traverse rows.
- Inside, compute something (sum, min, condition).
- Compare/update a tracking variable.
- Return a result.
Example types you should expect:
- Row with highest average
- Column with smallest sum
- Element that is min in its row and max in its column
- Game board neighbor checks (careful with bounds)
Break complex logic into helper methods if it makes your code clearer. The exam rewards clear structure.
When tracing code on multiple choice, draw a small grid and simulate the loops. Keep track of row and column carefully.
4. Common Mistakes That Cost Points
Row vs column confusion
- Rows →
arr.length - Columns →
arr[row].length
Off-by-one errors
- Use
< length - 1when checking neighbors. - Think carefully about
<vs<=when processing regions.
Forgetting to reset accumulators
Row sums must reset inside the outer loop.
Integer division in averages
Always cast if decimals matter.
Assuming square arrays
The AP often uses rectangular or jagged arrays. Never hardcode dimensions.