Problem overview
The method CenterOfGrid receives a 3x3 grid of integers as an int[][] and returns the number in the center: the one in the middle row and the middle column. For the grid with rows [1, 2, 3], [4, 5, 6] and [7, 8, 9] the answer is 5. For a grid of zeros with a 7 in the middle, it is 7. It can also be negative: in the grid with rows [9, 8, 7], [6, -5, 4] and [3, 2, 1] the center is -5.
Approach: two indexes
A grid is a table: rows, and in each row one number per column. In C# you can store it as an array of arrays, written int[][]. Each row is itself an int[]. This is called a jagged array, because the rows are separate arrays and could in principle have different lengths.
Two indexes pick a number. grid[row] is a whole row, and grid[row][col] is the number at that row and column. Counting starts at 0, so the rows are 0, 1 and 2, and the same for the columns. The middle row is row 1 and the middle column is column 1.
int[][] grid = new int[][] { new[] {1, 2, 3}, new[] {4, 5, 6}, new[] {7, 8, 9} };
Console.WriteLine(grid.Length);
Console.WriteLine(string.Join(",", grid[1]));
Console.WriteLine(grid[1][1]);
Console.WriteLine(grid[2][0]);
3
4,5,6
5
7
Here grid.Length is the number of rows, grid[1] is the middle row, grid[1][1] is its middle number, and grid[2][0] is the first number of the last row.
Step by step
- Pick the middle row.
grid[1]is[4, 5, 6]in the first example. - Pick the middle column of that row. Index 1 of
[4, 5, 6]is 5. - Return it. The two steps combine into
grid[1][1].
The solution in C#
class Solution {
public int CenterOfGrid(int[][] grid) {
return grid[1][1];
}
}
Each pair of brackets takes one level: the first selects the row, the second selects the number in it. The order is always row first, then column.
Jagged arrays and rectangular arrays
C# has a second kind of two-dimensional array, int[,], written with a comma. It is a single rectangular block, and its elements are read as grid[row, col]. The two kinds are not interchangeable. This lesson's parameter is int[][], so the comma form does not compile on it: the compiler reports that the number of indices is wrong.
Edge cases and common mistakes
- Counting from 1.
grid[2][2]is the bottom-right corner of a 3x3 grid, not the center. The center is at index 1 in both directions. - Swapping row and column. This does not matter at the center of a square grid, but
grid[2][0]andgrid[0][2]are different cells in general. - Going out of range. Reading
grid[3]throws anIndexOutOfRangeException, since the valid rows are 0 to 2. - Negative numbers. The center can be any integer between -1000 and 1000, and
intholds all of them, so the method needs no special case.
The same idea gives the center of any square grid with an odd side. For a grid with n rows, the middle row is n / 2 (integer division), so the center is grid[n / 2][n / 2]. With 3 rows, 3 / 2 is 1, which is why this lesson's answer is grid[1][1]. For a 5x5 grid it would be grid[2][2]. Writing the index as a fixed 1 is correct here only because the lesson fixes the size at 3x3.
Testing
| Grid rows | Result |
|---|---|
[1, 2, 3], [4, 5, 6], [7, 8, 9] | 5 |
[0, 0, 0], [0, 7, 0], [0, 0, 0] | 7 |
[9, 8, 7], [6, -5, 4], [3, 2, 1] | -5 |
