Untitled

Ask TA for the time complexity
mail@pastecode.io avatar
unknown
plain_text
a year ago
930 B
1
Indexable
Never
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.


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


Output - 
[1,2,0,0]
[0,0,0,0]
[0,0,0,0]

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

Solution -

const setToZero = (A, row, col) => {
    console.log("here");
    const rows = A.length;
    const columns = A[0].length;

    for (let i = 0; i < rows; i++) {
        A[i][col] = 0;
    }

    for (let j = 0; j < columns; j++) {
        A[row][j] = 0;
    }
    console.log(A)
    return A;
}

const solve = (A) => {
    const rows = A.length;
    const columns = A[0].length;

    for (let i = 0; i < rows; i++) {
        for (let j = 0; j < columns; j++) {
            if (A[i][j] === 0) {
                A = setToZero(A, i, j);
            }
        }
    }
    return A;
}