title: Java - Zero out the rows/columns of 2d array if it contains a target number
body: <p>Given a mxn matrix that looks like this:<br>
  1034<br>
  1234<br>
  3560</p>
  
  <p>Need to output something like this:<br>
  0000<br>
  1030<br>
  0000  </p>
  
  <p>*Target number is 0.</p>
  
  <p>Here's my solution, but I think it's not very efficient (both space and running time which I believe is O(m^2 * n)) and was wondering if there are simpler and more efficient ways to do this. If yes, what is it?</p>
  
  <pre><code>int[][] m = { { 1, 0, 3, 4 }, { 1, 2, 3, 4 }, { 3, 5, 6, 0 } };  
  m = zero(m, 0);
  
  public static int[][] zero(int[][] m, int num) {
  
      int rows = m.length;
      int columns = m[0].length;
      int [][] myInt = new int[rows][];
      for(int i = 0; i &lt; rows; i++){
          myInt[i] = m[i].clone();
      }
      for (int i = 0; i &lt; rows; i++) {
          for (int j = 0; j &lt; columns; j++) {
              if (myInt[i][j] == num) {
                  m[i] = new int[columns];
                  for(int k = 0; k &lt; rows; k++){
                      m[k][j] = 0;
                  }
                  break;
              }
          }
      }       
      return m;
  }
  </code></pre>
  
  <p>Basically I clone the input matrix first, then iterate through each row and check if the row contains my target number. If yes, then I set the entire row in the original matrix to zero. Then I do another loop to set the column which contains the target number to zero. I cloned the matrix in the beginning so the checking is always against the cloned reference matrix instead of the modified one at each iteration.</p>