Start learning free

LESSON 113 OF 285 · ARRAYS - CHALLENGES

Center of the Grid in Java: 2D Array Indexing

Practice Read the full explanation

Given a 3×3 grid of integers grid, return the number in its center: the one in the middle row and the middle column.

A grid is a table of numbers made of rows. This one has 3 rows, and every row has 3 numbers, one in each column. Rows and columns are counted from 0, so the middle row is row 1 and the middle column is column 1.

 

Example 1:

Input: grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: 5
Explanation: The middle row is [4, 5, 6], and the number in its middle column is 5.

Example 2:

Input: grid = [[0, 0, 0], [0, 7, 0], [0, 0, 0]]
Output: 7
Explanation: 7 is in row 1, column 1: the center of the grid.

Example 3:

Input: grid = [[9, 8, 7], [6, -5, 4], [3, 2, 1]]
Output: -5
Explanation: The center can be any number, a negative one too.

 

Constraints:

  • grid has exactly 3 rows, and every row has exactly 3 numbers.
  • Every number is an integer between -1000 and 1000.
STEP 1 OF 1
    Ctrl + Enter to check

    JAVA · ARRAYS - CHALLENGES

    Center Of The Grid in Java, explained

    Problem overview

    The method centerOfGrid receives grid, a 3x3 grid of integers, and returns the number in its center: the one in the middle row and the middle column. The grid has exactly 3 rows with 3 numbers each, and every number is between -1000 and 1000.

    For {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} the middle row is {4, 5, 6} and its middle number is 5. For a grid with a 7 in the middle surrounded by zeros, the answer is 7. For {{9, 8, 7}, {6, -5, 4}, {3, 2, 1}} it is -5: the center can be any number, including a negative one.

    Approach: two indexes

    In Java, a grid of integers has the type int[][]: an array whose items are themselves int[] arrays, one per row. This has two consequences for reading values.

    • grid[row] is a whole row, an int[].
    • grid[row][col] is one number: first pick the row, then pick the column inside that row.

    The first index always selects the row and the second selects the column. The picture to keep in mind is the grid as it is written in the statement, with the first inner array on top.

    Indexes start at 0, so a 3x3 grid has rows 0, 1 and 2 and columns 0, 1 and 2. The middle of 0, 1, 2 is 1, in both directions. The center is therefore at row 1, column 1.

    Step by step

    1. Understand the shape. For {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, grid[0] is {1, 2, 3}, grid[1] is {4, 5, 6} and grid[2] is {7, 8, 9}.
    2. Pick the middle row. grid[1] is the row {4, 5, 6}.
    3. Pick the middle column of that row. Indexing that row with [1] gives 5. Written in one go, this is grid[1][1].
    4. Return it. The method returns an int, and grid[1][1] is an int, so the value can be returned directly.

    The solution in Java

    class Solution {
        public int centerOfGrid(int[][] grid) {
            return grid[1][1];
        }
    }
    

    The method returns the following for the examples:

    [[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> 5
    [[9, 8, 7], [6, -5, 4], [3, 2, 1]] -> -5
    

    A few details about Java arrays are worth knowing:

    • Arrays have a fixed size. The size of an array is its length, a field with no parentheses, unlike String.length(). For this grid, grid.length is 3 (the number of rows) and grid[0].length is 3 (the number of columns).
    • Rows are separate arrays. In Java a 2D array doesn't have to be rectangular, so each row has its own length. This lesson promises 3 and 3, which is why a fixed [1][1] is safe.
    • Out-of-range indexes fail at run time. Reading grid[3][0] throws an ArrayIndexOutOfBoundsException. Java never silently returns a default value for a bad index.

    Edge cases and common mistakes

    • Counting from 1. Beginners often ask for row 2, column 2 because the middle is "the second". Index 2 is the third row, and grid[2][2] is the bottom-right corner: 9 in the first example.
    • Swapping the indexes. For a center, grid[1][1] reads the same either way, so the mistake stays hidden here. For other cells it matters: grid[0][2] is the top-right corner (3), while grid[2][0] is the bottom-left (7).
    • Dividing by two. A general rule for the middle index is length / 2 with integer division, which is 1 for a length of 3. Here the grid is always 3x3, so the constant 1 is enough, but with another size the constant would be wrong.
    • Misreading the value. The center is a position, not the biggest or the median number. In the second example the center is -5 even though the other numbers are larger.
    • Grids of a different shape. With a 2x2 grid, grid[1][1] is the bottom-right number and there is no center. The constraints exclude such inputs.

    Testing

    • {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} gives 5.
    • {{9, 8, 7}, {6, -5, 4}, {3, 2, 1}} gives -5.
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free