Data Structure & Algorithms
Advance DSA
Arrays Two Dimensional
Row to Column Zero

Row to Column Zero

Problem Description

You are given a 2D integer matrix A, make all the elements in a row or column zero if the A[i][j] = 0. Specifically, make entire ith row and jth column zero.

Problem Constraints

1 <= A.size() <= 10^3
1 <= A[i].size() <= 10^3
0 <= A[i][j] <= 10^3

Input Format

First argument is a 2D integer matrix A.

Output Format

Return a 2D matrix after doing required operations.

Example Input

Input 1:
[1,2,3,4]
[5,6,7,0]
[9,2,0,4]

Example Output

Output 1:
[1,2,0,0]
[0,0,0,0]
[0,0,0,0]

Example Explanation

Explanation 1:
A[2][4] = A[3][3] = 0, so make 2nd row, 3rd row, 3rd column and 4th column zero.

Output

Java
import java.util.*;
 
public class ZeroMatrix {
    public int[][] setZeroes(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        boolean firstRowHasZero = false;
        boolean firstColHasZero = false;
 
        // Check if the first row has a zero
        for (int j = 0; j < cols; j++) {
            if (matrix[0][j] == 0) {
                firstRowHasZero = true;
                break;
            }
        }
 
        // Check if the first column has a zero
        for (int i = 0; i < rows; i++) {
            if (matrix[i][0] == 0) {
                firstColHasZero = true;
                break;
            }
        }
 
        // Mark zeros on first row and first column
        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 zeros based on markings in first row and column
        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 zeros for first row if needed
        if (firstRowHasZero) {
            for (int j = 0; j < cols; j++) {
                matrix[0][j] = 0;
            }
        }
 
        // Set zeros for first column if needed
        if (firstColHasZero) {
            for (int i = 0; i < rows; i++) {
                matrix[i][0] = 0;
            }
        }
 
        return matrix;
    }
}
Python
def setZeroes(matrix):
    rows = len(matrix)
    cols = len(matrix[0])
    first_row_has_zero = False
    first_col_has_zero = False
 
    for j in range(cols):
        if matrix[0][j] == 0:
            first_row_has_zero = True
            break
 
    for i in range(rows):
        if matrix[i][0] == 0:
            first_col_has_zero = True
            break
 
    for i in range(1, rows):
        for j in range(1, cols):
            if matrix[i][j] == 0:
                matrix[i][0] = 0
                matrix[0][j] = 0
 
    for i in range(1, rows):
        for j in range(1, cols):
            if matrix[i][0] == 0 or matrix[0][j] == 0:
                matrix[i][j] = 0
 
    if first_row_has_zero:
        for j in range(cols):
            matrix[0][j] = 0
 
    if first_col_has_zero:
        for i in range(rows):
            matrix[i][0] = 0
 
    return matrix
JavaScript
function setZeroes(matrix) {
    const rows = matrix.length;
    const cols = matrix[0].length;
    let firstRowHasZero = false;
    let firstColHasZero = false;
 
    for (let j = 0; j < cols; j++) {
        if (matrix[0][j] === 0) {
            firstRowHasZero = true;
            break;
        }
    }
 
    for (let i = 0; i < rows; i++) {
        if (matrix[i][0] === 0) {
            firstColHasZero = true;
            break;
        }
    }
 
    for (let i = 1; i < rows; i++) {
        for (let j = 1; j < cols; j++) {
            if (matrix[i][j] === 0) {
                matrix[i][0] = 0;
                matrix[0][j] = 0;
            }
        }
    }
 
    for (let i = 1; i < rows; i++) {
        for (let j = 1; j < cols; j++) {
            if (matrix[i][0] === 0 || matrix[0][j] === 0) {
                matrix[i][j] = 0;
            }
        }
    }
 
    if (firstRowHasZero) {
        for (let j = 0; j < cols; j++) {
            matrix[0][j] = 0;
        }
    }
 
    if (firstColHasZero) {
        for (let i = 0; i < rows; i++) {
            matrix[i][0] = 0;
        }
    }
 
    return matrix;
}