2025 AP® Computer Science A FRQ 4 — SumOrSameGame (2D Arrays & Grid Search)

2025 AP® Computer Science A FRQ 4 — SumOrSameGame (2D Arrays & Grid Search)

Topic: 2D array initialization, random number generation, grid search algorithm
2025 Curriculum Alignment: Unit 4 (Data Collection — 2D Arrays)


Question

This question involves reasoning about a number puzzle that is represented as a two-dimensional array of integers. Each element of the array initially contains a value between 1 and 9, inclusive. Solving the puzzle involves clearing pairs of array elements by setting them to 0.

Two elements can be cleared if their values sum to 10 or if they have the same value. The puzzle is considered solved if all elements of the array are cleared.

You will write the constructor and one method of the SumOrSameGame class, which contains the methods that manipulate elements of the puzzle.

Provided Class

public class SumOrSameGame {
    private int[][] puzzle;
    
    /**
     * Creates a two-dimensional array and fills it with random integers,
     * as described in part (a)
     * Precondition: numRows > 0; numCols > 0
     */
    public SumOrSameGame(int numRows, int numCols) {
        /* to be implemented in part (a) */
    }
    
    /**
     * Identifies and clears an element of puzzle that can be paired with
     * the element at the given row and column, as described in part (b)
     * Preconditions: row and col are valid row and column indices in puzzle.
     *                The element at the given row and column is between 1 and 9, inclusive.
     */
    public boolean clearPair(int row, int col) {
        /* to be implemented in part (b) */
    }
    
    /* There may be instance variables, constructors,
       and methods that are not shown. */
}

Part (a)

Write the constructor for the SumOrSameGame class. The constructor initializes the instance variable puzzle to be a two-dimensional integer array with the number of rows and columns specified by the parameters numRows and numCols, respectively.

Array elements are initialized with random integers between 1 and 9, inclusive, each with an equal chance of being assigned to each element of puzzle.

When an element of the two-dimensional array is accessed, the first index is used to specify the row and the second index is used to specify the column.

Complete the SumOrSameGame constructor.

Part (b)

Write the clearPair method, which takes a valid row index and valid column index as its parameters. The array element specified by those indices, which has a value between 1 and 9, inclusive, is compared to other array elements in puzzle in an attempt to pair it with another array element that meets both of the following conditions:

  • The row index of the second element is greater than or equal to the parameter row.
  • The two elements have equal values or have values that sum to 10.

If such an array element is found, both array elements of the pair are cleared (set to 0) and the method returns true. If more than one such array element is found, any one of those identified array elements can be used to complete the pair and can be cleared. If no such array element is found, no changes are made to puzzle and the method returns false.

Examples: The following table shows the possible results of several calls to clearPair:

puzzle before call Method Call puzzle after call Return Explanation
0 7 9 0
7 4 1 6
8 4 1 8
clearPair(0, 1)
0 0 9 0
0 4 1 6
8 4 1 8
true Value 7 at (0,1) matched with 7 at (1,0)
1 2 3 4
5 6 7 8
5 4 1 2
clearPair(2, 2)
1 2 3 4
5 6 7 8
5 4 1 2
false No element in row 2 or later to pair with value 1
8 1 0 5
0 4 3 6
3 4 5 8
clearPair(1, 1)
8 1 0 5
0 0 3 0
3 4 5 8
true Value 4 at (1,1) matched with 6 at (1,3). Could also match with 4 at (2,1)
1 7 9
2 6 5
4 4 4
clearPair(0, 2)
0 7 0
2 6 5
4 4 4
true Value 9 at (0,2) matched with 1 at (0,0). Note: checks same row too!

Complete method clearPair.


Solution & Explanation

Part (a) Solution

public SumOrSameGame(int numRows, int numCols) {
    puzzle = new int[numRows][numCols];
    
    for (int row = 0; row < puzzle.length; row++) {
        for (int col = 0; col < puzzle[row].length; col++) {
            puzzle[row][col] = (int) (Math.random() * 9) + 1;
        }
    }
}

Part (a) Explanation

Algorithm Logic:

  1. Initialize puzzle to a new 2D array with numRows rows and numCols columns
  2. Use nested loops to traverse every element:
    • Outer loop: iterate through rows (0 to puzzle.length - 1)
    • Inner loop: iterate through columns (0 to puzzle[row].length - 1)
  3. For each element, assign a random integer from 1 to 9

Random Number Generation:

Formula: (int) (Math.random() * 9) + 1

Step Expression Range
1. Math.random() Math.random() [0.0, 1.0)
2. Multiply by 9 Math.random() * 9 [0.0, 9.0)
3. Cast to int (int) (Math.random() * 9) [0, 8]
4. Add 1 (int) (Math.random() * 9) + 1 [1, 9]

Key Points:

  • 2D array initialization: new int[numRows][numCols] creates rows first, then columns
  • Accessing dimensions: puzzle.length = number of rows, puzzle[row].length = number of columns in that row
  • Parentheses matter: Must cast to int AFTER multiplying: (int) (Math.random() * 9)

Part (b) Solution

public boolean clearPair(int row, int col) {
    int value = puzzle[row][col];
    
    // Search from current row to end
    for (int r = row; r < puzzle.length; r++) {
        // Determine starting column
        int startCol = (r == row) ? col + 1 : 0;
        
        // Search columns in this row
        for (int c = startCol; c < puzzle[r].length; c++) {
            int otherValue = puzzle[r][c];
            
            // Check if values match or sum to 10
            if (value == otherValue || value + otherValue == 10) {
                puzzle[row][col] = 0;
                puzzle[r][c] = 0;
                return true;
            }
        }
    }
    
    return false;
}

Part (b) Explanation

Algorithm Logic:

  1. Store the value at the given position: value = puzzle[row][col]
  2. Search from the given row to the last row
  3. For each row being searched:
    • If searching the SAME row as the starting position: begin at col + 1 (don't compare element with itself)
    • If searching a DIFFERENT row: begin at column 0
  4. For each element in the search range:
    • Check if values are equal OR sum to 10
    • If match found: set both elements to 0 and return true
  5. If no match found after searching entire range: return false

Critical Detail - Starting Column:

int startCol = (r == row) ? col + 1 : 0;

This ternary operator handles two cases:

  • Same row (r == row): Start at col + 1 to avoid comparing element with itself
  • Different row (r != row): Start at column 0

Detailed Example Walkthrough

Example: clearPair(0, 1) with this grid:

0 7
(0,1)
9 0
7
(1,0)
4 1 6
8 4 1 8

Search Process:

r c Position Value Match?
0 2 (0,2) 9 No (7 ≠ 9, 7+9 ≠ 10)
0 3 (0,3) 0 No
1 0 (1,0) 7 YES! (7 == 7)

Result: Clear (0,1) and (1,0), return true 


Scoring Notes

Part (a) — 4 points:

  • +1: Initializes puzzle to correct 2D array size
  • +1: Uses nested loops to access all elements
  • +1: Generates random integers correctly
  • +1: Assigns values in range 1-9 inclusive

Part (b) — 5 points:

  • +1: Searches from row parameter to end of array
  • +1: Determines correct starting column for each row
  • +1: Checks both conditions (equal values OR sum to 10)
  • +1: Sets both elements to 0 when match found
  • +1: Returns correct boolean value

Common Mistakes to Avoid:

  • Part (a): Using (int) Math.random() * 9 instead of (int) (Math.random() * 9) (wrong order of operations)
  • Part (a): Generating numbers from 0-8 or 0-9 instead of 1-9
  • Part (b): Comparing element with itself (not starting at col + 1 for same row)
  • Part (b): Only checking rows AFTER the given row (should check same row too)
  • Part (b): Using && instead of || for the condition check
  • Part (b): Forgetting to clear BOTH elements of the pair

College Board Resources

Contact form