Set Matrix Zeroes In Place in DSA Java - Time & Space Complexity
We want to know how the time needed to set matrix zeroes grows as the matrix size grows.
How does the number of steps change when the matrix gets bigger?
Analyze the time complexity of the following code snippet.
public void setZeroes(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
boolean firstColZero = false;
for (int i = 0; i < rows; i++) {
if (matrix[i][0] == 0) firstColZero = true;
for (int j = 1; j < cols; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
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;
}
}
}
if (matrix[0][0] == 0) {
for (int j = 0; j < cols; j++) matrix[0][j] = 0;
}
if (firstColZero) {
for (int i = 0; i < rows; i++) matrix[i][0] = 0;
}
}
This code sets entire rows and columns to zero if any element in them is zero, using the matrix itself to store flags.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops scanning all matrix elements multiple times.
- How many times: Each element is checked or updated a few times, but loops run over all rows and columns.
As the matrix size grows, the number of steps grows roughly with the number of elements.
| Input Size (n x m) | Approx. Operations |
|---|---|
| 10 x 10 | About 300 steps |
| 100 x 100 | About 30,000 steps |
| 1000 x 1000 | About 3,000,000 steps |
Pattern observation: Operations grow roughly proportional to the total number of elements (rows times columns).
Time Complexity: O(n * m)
This means the time needed grows directly with the number of elements in the matrix.
[X] Wrong: "Since we use nested loops, the time complexity must be quadratic like O(n²) always."
[OK] Correct: Here, n and m are different dimensions (rows and columns). The complexity depends on both, so it is O(n * m), not necessarily square if rows and columns differ.
Understanding how nested loops over a matrix affect time helps you explain and optimize matrix problems clearly in interviews.
"What if we used extra space to store zero flags instead of the matrix itself? How would the time complexity change?"
Practice
Set Matrix Zeroes In Place problem?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.Step 2: Recognize the constraint of in-place modification
We must do this without using extra space for another matrix or large data structures.Final Answer:
Modify the matrix so that if an element is 0, its entire row and column become 0 without using extra space. -> Option BQuick Check:
Goal = Set rows and columns to zero in-place [OK]
- Using extra space like another matrix
- Only zeroing rows or columns, not both
- Not modifying the matrix in-place
matrix with 3 rows and 4 columns?Solution
Step 1: Recall Java 2D array declaration syntax
In Java, 2D arrays are declared asint[][] name = new int[rows][columns];.Step 2: Match the correct dimensions
int[][] matrix = new int[3][4]; uses 3 rows and 4 columns correctly. int matrix[][] = new int[4][3]; reverses dimensions, C is invalid syntax, D misses array brackets.Final Answer:
int[][] matrix = new int[3][4]; -> Option AQuick Check:
Java 2D array syntax = int[][] matrix = new int[3][4]; [OK]
- Swapping rows and columns in new int[][]
- Using C-style array declaration
- Missing brackets in declaration
int[][] matrix = {
{1, 2, 0},
{4, 5, 6},
{7, 0, 9}
};
// After applying setZeroes(matrix) method
// (method sets entire row and column to zero if element is zero)
Solution
Step 1: Identify zero elements and their rows and columns
Zeros are at positions (0,2) and (2,1). Rows 0 and 2, columns 1 and 2 must be zeroed.Step 2: Set entire rows and columns to zero
Row 0 becomes [0,0,0], row 2 becomes [0,0,0]. Column 1 and 2 in row 1 become 0, so row 1 becomes [4,0,0].Final Answer:
[[0, 0, 0], [4, 0, 0], [0, 0, 0]] -> Option AQuick Check:
Zero rows and columns set correctly = [[0, 0, 0], [4, 0, 0], [0, 0, 0]] [OK]
- Only zeroing rows or columns, not both
- Not updating all affected cells
- Overwriting zeros too early
public void setZeroes(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
boolean[] rowZero = new boolean[rows];
boolean[] colZero = new boolean[cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) {
rowZero[i] = true;
colZero[j] = true;
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (rowZero[i] || colZero[j]) {
matrix[i][j] = 0;
}
}
}
}
What is the main issue with this code regarding the problem constraints?Solution
Step 1: Analyze space usage of the code
The code uses two boolean arrays, rowZero and colZero, sized by rows and columns, which is extra space.Step 2: Compare with problem constraints
The problem requires in-place modification without extra space, so using these arrays violates the constraint.Final Answer:
It uses extra space proportional to rows and columns, violating in-place requirement. -> Option DQuick Check:
Extra space used = violates in-place [OK]
- Ignoring space constraints
- Using extra arrays for marking
- Not handling first row/column carefully
1 2 3 4 5 0 7 8 9 10 11 12 0 14 15 16Using the in-place method for setting matrix zeroes, what will be the matrix after the operation?
Solution
Step 1: Identify zero positions and mark rows and columns
Zeros at (1,1) and (3,0). Rows 1 and 3, columns 0 and 1 must be zeroed.Step 2: Set zeros for affected rows and columns
Rows 1 and 3 become entirely zeros. Columns 0 and 1 become zeros across all rows, resulting in row 0: [0,0,3,4]; row 2: [0,0,11,12].Final Answer:
[[0, 0, 3, 4], [0, 0, 0, 0], [0, 0, 11, 12], [0, 0, 0, 0]] -> Option CQuick Check:
Zero rows and columns updated correctly = [[0, 0, 3, 4], [0, 0, 0, 0], [0, 0, 11, 12], [0, 0, 0, 0]] [OK]
- Zeroing entire rows or columns unnecessarily
- Missing zeros in some cells
- Not handling first row and column markers properly
Ready to prove it?
Pass the test and this pattern moves unknown
