int[][] matrix = new int[][] { new int[] {1, 2, 3}, new int[] {4, 0, 6}, new int[] {7, 8, 9} }; SetZeroes(matrix); // Print matrix for (int i = 0; i < matrix.Length; i++) { for (int j = 0; j < matrix[0].Length; j++) { Console.Write(matrix[i][j] + " "); } Console.WriteLine(); } void SetZeroes(int[][] mat) { int m = mat.Length, n = mat[0].Length; bool firstRowZero = false, firstColZero = false; for (int i = 0; i < m; i++) { if (mat[i][0] == 0) firstColZero = true; } for (int j = 0; j < n; j++) { if (mat[0][j] == 0) firstRowZero = true; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (mat[i][j] == 0) { mat[i][0] = 0; mat[0][j] = 0; } } } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (mat[i][0] == 0 || mat[0][j] == 0) { mat[i][j] = 0; } } } if (firstColZero) { for (int i = 0; i < m; i++) mat[i][0] = 0; } if (firstRowZero) { for (int j = 0; j < n; j++) mat[0][j] = 0; } }
The code first checks if the first row or first column has any zeroes and stores that info. Then it uses the first row and column to mark which rows and columns should be zeroed. Finally, it zeroes the marked rows and columns except the first row and column, which are handled last based on the initial flags.
In this example, only the middle row and second column get zeroed except the first row and column remain mostly unchanged except where marked.
int[][] matrix = new int[][] { new int[] {0, 1, 2, 0}, new int[] {3, 4, 5, 2}, new int[] {1, 3, 1, 5} }; SetZeroes(matrix); // Print matrix for (int i = 0; i < matrix.Length; i++) { for (int j = 0; j < matrix[0].Length; j++) { Console.Write(matrix[i][j] + " "); } Console.WriteLine(); } void SetZeroes(int[][] mat) { int m = mat.Length, n = mat[0].Length; bool firstRowZero = false, firstColZero = false; for (int i = 0; i < m; i++) { if (mat[i][0] == 0) firstColZero = true; } for (int j = 0; j < n; j++) { if (mat[0][j] == 0) firstRowZero = true; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (mat[i][j] == 0) { mat[i][0] = 0; mat[0][j] = 0; } } } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (mat[i][0] == 0 || mat[0][j] == 0) { mat[i][j] = 0; } } } if (firstColZero) { for (int i = 0; i < m; i++) mat[i][0] = 0; } if (firstRowZero) { for (int j = 0; j < n; j++) mat[0][j] = 0; } }
The first row and first column contain zeroes, so they will be zeroed entirely. The middle row and last column get zeroed where marked. The final matrix reflects these zeroes.
int[][] matrix = new int[][] { new int[] {1, 2}, new int[] {3, 0}, new int[] {5, 6} }; SetZeroes(matrix); void SetZeroes(int[][] mat) { int m = mat.Length, n = mat[0].Length; bool firstRowZero = false, firstColZero = false; for (int i = 0; i < m; i++) { if (mat[i][0] == 0) firstColZero = true; } for (int j = 0; j < n; j++) { if (mat[0][j] == 0) firstRowZero = true; } // rest of code omitted for brevity }
The loop iterates with i <= m, but valid indices are 0 to m-1. Accessing mat[m][0] causes IndexOutOfRangeException.
int[][] matrix = new int[][] { new int[] {1, 0, 3}, new int[] {4, 5, 6}, new int[] {7, 8, 0} }; SetZeroes(matrix); // Print matrix for (int i = 0; i < matrix.Length; i++) { for (int j = 0; j < matrix[0].Length; j++) { Console.Write(matrix[i][j] + " "); } Console.WriteLine(); } void SetZeroes(int[][] mat) { int m = mat.Length, n = mat[0].Length; bool firstRowZero = false, firstColZero = false; for (int i = 0; i < m; i++) { if (mat[i][0] == 0) firstColZero = true; } for (int j = 0; j < n; j++) { if (mat[0][j] == 0) firstRowZero = true; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (mat[i][j] == 0) { mat[i][0] = 0; mat[0][j] = 0; } } } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (mat[i][0] == 0 || mat[0][j] == 0) { mat[i][j] = 0; } } } if (firstColZero) { for (int i = 0; i < m; i++) mat[i][0] = 0; } if (firstRowZero) { for (int j = 0; j < n; j++) mat[0][j] = 0; } }
The first row has a zero at index 1, so firstRowZero is true. The first column has no zero, so firstColZero is false. The zero at matrix[2][2] marks row 2 and column 2 to be zeroed. After processing, the first row and column zeroing is applied last.
int[][] matrix = new int[][] { new int[] {1, 2, 3, 4}, new int[] {5, 0, 7, 8}, new int[] {9, 10, 11, 12}, new int[] {0, 14, 15, 16} }; SetZeroes(matrix); int zeroCount = 0; for (int i = 0; i < matrix.Length; i++) { for (int j = 0; j < matrix[0].Length; j++) { if (matrix[i][j] == 0) zeroCount++; } } Console.WriteLine(zeroCount); void SetZeroes(int[][] mat) { int m = mat.Length, n = mat[0].Length; bool firstRowZero = false, firstColZero = false; for (int i = 0; i < m; i++) { if (mat[i][0] == 0) firstColZero = true; } for (int j = 0; j < n; j++) { if (mat[0][j] == 0) firstRowZero = true; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (mat[i][j] == 0) { mat[i][0] = 0; mat[0][j] = 0; } } } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (mat[i][0] == 0 || mat[0][j] == 0) { mat[i][j] = 0; } } } if (firstColZero) { for (int i = 0; i < m; i++) mat[i][0] = 0; } if (firstRowZero) { for (int j = 0; j < n; j++) mat[0][j] = 0; } }
The zero at matrix[1][1] marks row 1 and column 1. The zero at matrix[3][0] marks first column. After zeroing, the zeroes appear in row 1, column 1, and first column. Counting carefully, total zeroes are 9.
