Untitled

Ask TA for the time complexity
 avatar
unknown
plain_text
2 years ago
930 B
2
Indexable
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;
}