📖 Scenario: You are working on a spreadsheet application. Sometimes, if a cell has a zero, the entire row and column should be cleared (set to zero) to indicate missing data.
🎯 Goal: Write a program that modifies a given 2D matrix in place. If any cell is zero, set its entire row and column to zero without using extra space for another matrix.
📋 What You'll Learn
Create a 2D integer array called matrix with the exact values given.
Create two boolean variables firstRowHasZero and firstColHasZero to track zeros in the first row and column.
Use the first row and first column of matrix to mark which rows and columns should be zeroed.
Modify matrix in place to set rows and columns to zero based on the markers.
Print the final state of matrix row by row, with values separated by spaces.
💡 Why This Matters
🌍 Real World
Spreadsheet applications and data cleaning often require marking entire rows and columns as invalid or missing based on certain cell values.
💼 Career
Understanding in-place matrix manipulation is useful for software engineers working with image processing, data analysis, and optimization problems.
Progress0 / 4 steps
1
Create the initial matrix
Create a 2D integer array called matrix with these exact values: 1, 1, 1, 1, 0, 1, 1, 1, 1.
DSA C#
Hint
Use a jagged array in C# to create matrix with three rows and three columns.
2
Check first row and column for zeros
Create two boolean variables called firstRowHasZero and firstColHasZero. Set firstRowHasZero to true if any element in the first row of matrix is zero. Set firstColHasZero to true if any element in the first column of matrix is zero.
DSA C#
Hint
Use loops to check the first row and first column for zeros and set the boolean variables accordingly.
3
Mark rows and columns to be zeroed using first row and column
Use nested for loops with variables i and j starting from 1 to iterate over matrix. If matrix[i][j] is zero, set matrix[i][0] and matrix[0][j] to zero to mark the row and column.
DSA C#
Hint
Start loops from 1 to avoid overwriting the first row and column markers.
4
Set matrix zeroes in place and print the result
Use nested for loops with variables i and j starting from 1 to set matrix[i][j] to zero if matrix[i][0] or matrix[0][j] is zero. Then, if firstRowHasZero is true, set all elements in the first row to zero. If firstColHasZero is true, set all elements in the first column to zero. Finally, print each row of matrix with values separated by spaces.
DSA C#
Hint
Use the markers in the first row and column to set zeros in the matrix. Don't forget to update the first row and column if needed. Finally, print each row with spaces between numbers.
Practice
(1/5)
1. What is the main goal of the Set Matrix Zeroes In Place problem?
easy
A. Replace all zeroes with ones in the matrix.
B. Count the number of zeroes in the matrix.
C. Modify the matrix so that if an element is 0, its entire row and column become 0 without using extra space.
D. Sort each row of the matrix in ascending order.
Solution
Step 1: Understand the problem requirement
The problem asks to set entire rows and columns to zero if any element in them is zero, modifying the matrix directly without extra space.
Step 2: Differentiate from other operations
Counting zeroes, replacing zeroes with ones, or sorting rows do not meet the problem's goal.
Final Answer:
Modify the matrix so that if an element is 0, its entire row and column become 0 without using extra space. -> Option C
Quick Check:
Goal is to set rows and columns zero in place = A [OK]
Hint: Focus on zero elements affecting rows and columns without extra space [OK]
Common Mistakes:
Thinking the problem is about counting zeroes
Trying to use extra memory like new arrays
Confusing zero replacement with sorting
2. Which of the following C# code snippets correctly declares a 2D integer matrix named matrix with 3 rows and 3 columns?
easy
A. int[,] matrix = new int[3,3];
B. int[][] matrix = new int[3][3];
C. int matrix[3,3];
D. int[] matrix = new int[3,3];
Solution
Step 1: Recall C# 2D array syntax
In C#, a rectangular 2D array is declared as int[,] matrix = new int[3,3];.
Step 2: Identify incorrect syntax
int[][] matrix = new int[3][3]; uses jagged array syntax incorrectly, C is invalid in C#, and D has type mismatch.
Final Answer:
int[,] matrix = new int[3,3]; -> Option A
Quick Check:
Correct 2D array declaration = A [OK]
Hint: Use int[,] for 2D arrays, not jagged or invalid syntax [OK]
Common Mistakes:
Using jagged array syntax incorrectly
Missing array type in declaration
Using C/C++ style array declaration
3. What will be the state of the matrix after running the following C# code?
int[,] matrix = {
{1, 2, 3},
{4, 0, 6},
{7, 8, 9}
};
// Set matrix zeroes in place
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
bool firstRowZero = false;
bool firstColZero = false;
// Check first row
for (int j = 0; j < cols; j++) {
if (matrix[0, j] == 0) firstRowZero = true;
}
// Check first column
for (int i = 0; i < rows; i++) {
if (matrix[i, 0] == 0) firstColZero = true;
}
// Use first row and column as markers
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (matrix[i, j] == 0) {
matrix[i, 0] = 0;
matrix[0, j] = 0;
}
}
}
// Set zeroes based on markers
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (matrix[i, 0] == 0 || matrix[0, j] == 0) {
matrix[i, j] = 0;
}
}
}
// Set first row zero if needed
if (firstRowZero) {
for (int j = 0; j < cols; j++) matrix[0, j] = 0;
}
// Set first column zero if needed
if (firstColZero) {
for (int i = 0; i < rows; i++) matrix[i, 0] = 0;
}
medium
A. [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
B. [[1, 0, 3], [0, 0, 0], [7, 0, 9]]
C. [[1, 0, 3], [0, 0, 0], [7, 0, 0]]
D. [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Solution
Step 1: Identify zero positions and mark rows and columns
Zero found at matrix[1,1]. Mark matrix[1,0] and matrix[0,1] as zero.
Step 2: Set zeroes based on markers
Set matrix[1,j] and matrix[i,1] to zero for i,j > 0. First row and column have no zero, so no change there.
Final Answer:
[[1, 0, 3], [0, 0, 0], [7, 0, 9]] -> Option B
Quick Check:
Zero at (1,1) sets row 1 and col 1 zero = A [OK]
Hint: Mark zero rows and columns using first row/column as flags [OK]
Common Mistakes:
Not marking first row and column correctly
Setting zeroes before marking all zero positions
Overwriting first row/column too early
4. The following code attempts to set matrix zeroes in place but has a bug. What is the bug?
int[,] matrix = {
{1, 0, 3},
{4, 5, 6},
{7, 8, 9}
};
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
bool firstRowZero = false;
bool firstColZero = false;
// Check first row
for (int j = 0; j < cols; j++) {
if (matrix[0, j] == 0) firstRowZero = true;
}
// Check first column
for (int i = 0; i < rows; i++) {
if (matrix[i, 0] == 0) firstColZero = true;
}
// Use first row and column as markers
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (matrix[i, j] == 0) {
matrix[i, 0] = 0;
matrix[0, j] = 0;
}
}
}
// Set zeroes based on markers
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (matrix[i, 0] == 0 || matrix[0, j] == 0) {
matrix[i, j] = 0;
}
}
}
// Set first row zero if needed
if (firstRowZero) {
for (int j = 0; j < cols; j++) matrix[0, j] = 0;
}
// Set first column zero if needed
if (firstColZero) {
for (int i = 0; i < rows; i++) matrix[i, 0] = 0;
}
medium
A. The first row zero check is done after modifying the matrix, causing incorrect results.
B. The first column zero check is missing.
C. The code does not mark zeroes in the first row and column correctly.
D. The code does not handle the case when the first element matrix[0,0] is zero.
Solution
Step 1: Understand the role of matrix[0,0]
matrix[0,0] is used to mark both first row and first column zero status, but here separate booleans are used.
Step 2: Identify missing handling of matrix[0,0]
If matrix[0,0] is zero, it affects both first row and column, but code only uses booleans, so it may miss this case.
Final Answer:
The code does not handle the case when the first element matrix[0,0] is zero. -> Option D
Quick Check:
matrix[0,0] zero affects both row and column flags = C [OK]
Hint: Remember matrix[0,0] marks both first row and column zero [OK]