0

I'm new to coding and I'm currently trying to solve this question: https://leetcode.com/problems/set-matrix-zeroes/

Code:

class Solution {
    public void setZeroes(int[][] matrix) {
        int [][] arr = matrix;
        for(int m=0;m<matrix.length;m++) {
            for(int n = 0;n<matrix[m].length;n++) {
                if(matrix[m][n] == 0) {
                    //loop 1
                    for(int i = 0; i<matrix.length;i++) {
                        arr[i][n] = 0;
                    }

                    //loop 2
                    for(int i = 0; i<matrix[m].length;i++) {
                        arr[m][i] = 0;
                    }
                }
            }
        }
        matrix = arr;
    }
}

Working code:

class Solution {
    public void setZeroes(int[][] matrix) {
        //declaring new 2D array
        int[][] arr = new int[matrix.length][matrix[0].length];
        
        //giving values to new 2D array (copying matrix)
        for(int i = 0; i<matrix.length;i++) {
            for(int j = 0; j<matrix[i].length;j++) {
                arr[i][j] = matrix[i][j];
            }
        }
        
        for(int m=0;m<matrix.length;m++) {
            for(int n = 0;n<matrix[m].length;n++) {
                if(matrix[m][n] == 0) {
                    for(int i = 0; i<matrix.length;i++) {
                        arr[i][n] = 0;
                    }
                    
                    for(int i = 0; i<matrix[m].length;i++) {
                        arr[m][i] = 0;
                    }
                }
            }
        }
        for(int i = 0; i<matrix.length;i++) {
            for(int j = 0; j<matrix[i].length;j++) {
                matrix[i][j] = arr[i][j];
            }
        }
    }
}

The problem I'm having is that the for loops are working fine separately, but when I run them together, they aren't working. If you remove loop 1, the loop 2 works fine.

The first for loop (loop 1) adds '0' vertically wherever they belong, and the second for loop (loop 2) adds '0' horizontally like its supposed to. But together, they don't work. What's the issue that I'm having?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    you are possibly overwriting values. This doesn't mean the loops don't work – Stultuske Jul 26 '22 at 08:32
  • can you provide a sample input and a sample output of the code (when it doesn't work)? – The Blind Hawk Jul 26 '22 at 08:47
  • 1
    @TheBlindHawk input: [[0,1,2,0],[3,4,5,2],[1,3,1,5]] output: [[0,0,0,0],[0,0,0,0],[0,0,0,0]] expected output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]] – Utkarsh Dogra Jul 26 '22 at 08:56
  • @user16320675 Loop 1 adds '0' vertically to the matrix, loop 2 does that horizontally like they're supposed to if either one of these is removed. But they don't work when both they both loop one after the other – Utkarsh Dogra Jul 26 '22 at 08:59
  • @Utkarsh Dogra did you check out my answer? The code should work if you declare ```int [][] arr``` as a different object. – The Blind Hawk Jul 26 '22 at 09:24
  • @user16320675 I posted the working code below the original code, mistake was not declaring new 2d array – Utkarsh Dogra Jul 26 '22 at 09:45

1 Answers1

1

The issue has to do with this:

int [][] arr = matrix;

this does NOT make a new matrix, but instead makes arr reference matrix.
If I am not mistaken you always need to declare the new matrix like this:

int [][] arr = new ....

check out this thread for more information as top how to duplicate a matrix.

The reason it worked when it was only one loop, was because you did not need to duplicate the matrix if you were running only one loop.

The Blind Hawk
  • 1,199
  • 8
  • 24