first-col
class Solution {
public void zero(int[][] arr) {
int m = arr.length;
int n = arr[0].length;
boolean fz = false;
boolean cz = false;
// Step 1: Check if first row has zero
for (int j = 0; j < n; j++) {
if (arr[0][j] == 0) {
fz = true;
break;
}
}
// Step 2: Check if first column has zero
for (int i = 0; i < m; i++) {
if (arr[i][0] == 0) {
cz = true;
break;
}
}
// Step 3: Mark zeros in first row and column
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (arr[i][j] == 0) {
arr[i][0] = 0; // Mark row
arr[0][j] = 0; // Mark column
}
}
}
// Step 4: Zero out cells based on markers
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (arr[i][0] == 0 || arr[0][j] == 0) {
arr[i][j] = 0;
}
}
}
// Step 5: Handle first row
if (fz) {
for (int j = 0; j < n; j++) {
arr[0][j] = 0;
}
}
// Step 6: Handle first column
if (cz) {
for (int i = 0; i < m; i++) {
arr[i][0] = 0;
}
}
}
}