Start learning free

LESSON 115 OF 289 · ARRAYS - CHALLENGES

Center of the Grid in C#: Reading a Jagged Array

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

    C# · ARRAYS - CHALLENGES

    Center Of The Grid in C#, explained

    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

    1. Pick the middle row. grid[1] is [4, 5, 6] in the first example.
    2. Pick the middle column of that row. Index 1 of [4, 5, 6] is 5.
    3. 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] and grid[0][2] are different cells in general.
    • Going out of range. Reading grid[3] throws an IndexOutOfRangeException, since the valid rows are 0 to 2.
    • Negative numbers. The center can be any integer between -1000 and 1000, and int holds 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 rowsResult
    [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
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free