Row sums and column sums

Sum each row

java
int[][] grid = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

for (int r = 0; r < grid.length; r++) {
    int rowSum = 0;
    for (int c = 0; c < grid[r].length; c++) {
        rowSum += grid[r][c];
    }
    System.out.println("Row " + r + " sum: " + rowSum);
}
Row 0 sum: 6
Row 1 sum: 15
Row 2 sum: 24

Sum each column

java
for (int c = 0; c < grid[0].length; c++) {
    int colSum = 0;
    for (int r = 0; r < grid.length; r++) {
        colSum += grid[r][c];
    }
    System.out.println("Col " + c + " sum: " + colSum);
}
Col 0 sum: 12
Col 1 sum: 15
Col 2 sum: 18

Searching in a 2D array

Find a specific value

java
public static boolean contains(int[][] grid, int target) {
    for (int[] row : grid) {
        for (int val : row) {
            if (val == target) {
                return true;
            }
        }
    }
    return false;
}

Find the position of a value

java
public static int[] findPosition(int[][] grid, int target) {
    for (int r = 0; r < grid.length; r++) {
        for (int c = 0; c < grid[r].length; c++) {
            if (grid[r][c] == target) {
                return new int[]{r, c};
            }
        }
    }
    return null;  // not found
}

int[] pos = findPosition(grid, 5);
System.out.println("Found at row " + pos[0] + ", col " + pos[1]);
// Found at row 1, col 1

Finding max/min in entiregrid

java
public static int findMax(int[][] grid) {
    int max = grid[0][0];
    for (int[] row : grid) {
        for (int val : row) {
            if (val > max) {
                max = val;
            }
        }
    }
    return max;
}

Replacing values

java
// Replace all negatives with 0
for (int r = 0; r < grid.length; r++) {
    for (int c = 0; c < grid[r].length; c++) {
        if (grid[r][c] < 0) {
            grid[r][c] = 0;
        }
    }
}

Counting elements

java
// Count how many elements are above a threshold
public static int countAbove(int[][] grid, int threshold) {
    int count = 0;
    for (int[] row : grid) {
        for (int val : row) {
            if (val > threshold) {
                count++;
            }
        }
    }
    return count;
}

Checking neighbors

java
public static int countNeighbors(int[][] grid, int r, int c) {
    int count = 0;
    // Check all 4 directions: up, down, left, right
    if (r > 0 && grid[r - 1][c] != 0) count++;                          // up
    if (r < grid.length - 1 && grid[r + 1][c] != 0) count++;            // down
    if (c > 0 && grid[r][c - 1] != 0) count++;                          // left
    if (c < grid[r].length - 1 && grid[r][c + 1] != 0) count++;         // right
    return count;
}

Transposing a matrix

java
public static int[][] transpose(int[][] matrix) {
    int rows = matrix.length;
    int cols = matrix[0].length;
    int[][] result = new int[cols][rows];
    
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            result[c][r] = matrix[r][c];
        }
    }
    return result;
}
[ 1  2  3 ]
[ 4  5  6 ]
[ 1  4 ]
[ 2  5 ]
[ 3  6 ]

Converting 1D index to 2D position

java
// Given a 1D index and the number of columns:
int index = 7;
int cols = 4;

int row = index / cols;    // 7 / 4 = 1
int col = index % cols;    // 7 % 4 = 3

// So index 7 maps to grid[1][3]
Index:  0  1  2  3
        4  5  6  7   ← index 7 = row 1, col 3
        8  9 10 11

Border elements

java
// Process only border elements of a grid
for (int r = 0; r < grid.length; r++) {
    for (int c = 0; c < grid[r].length; c++) {
        boolean isBorder = (r == 0 || r == grid.length - 1 || 
                           c == 0 || c == grid[r].length - 1);
        if (isBorder) {
            System.out.print(grid[r][c] + " ");
        }
    }
}
[*  *  *]
[*     *]
[*  *  *]

FRQ-style example: Image processing

java
public class ImageProcessor {
    private int[][] pixels;  // 0-255 grayscale values
    
    public ImageProcessor(int[][] img) {
        pixels = img;
    }
    
    /** Returns the average brightness of the entire image */
    public double getAverageBrightness() {
        int sum = 0;
        int count = 0;
        for (int[] row : pixels) {
            for (int p : row) {
                sum += p;
                count++;
            }
        }
        return (double) sum / count;
    }
    
    /** Inverts all pixel values (negative image) */
    public void invert() {
        for (int r = 0; r < pixels.length; r++) {
            for (int c = 0; c < pixels[r].length; c++) {
                pixels[r][c] = 255 - pixels[r][c];
            }
        }
    }
    
    /** Returns a new image that is a horizontal mirror */
    public int[][] mirror() {
        int rows = pixels.length;
        int cols = pixels[0].length;
        int[][] result = new int[rows][cols];
        
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                result[r][c] = pixels[r][cols - 1 - c];
            }
        }
        return result;
    }
    
    /** Counts pixels brighter than the given threshold */
    public int countBright(int threshold) {
        int count = 0;
        for (int[] row : pixels) {
            for (int p : row) {
                if (p > threshold) count++;
            }
        }
        return count;
    }
}

AP Exam Tips

Common Mistakes

Key Vocabulary