@@ -0,0 +1,1923 @@
+package com.sunny.grokkingalgorithms.ctci.c1;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A/**%0A * Created by sundas on 3/13/2018.%0A */%0Aclass Holder2D%7B%0A   public int i;%0A   public int j;%0A%7D%0Apublic class SetRowAndColumnToZero %7B%0A%0A  /*%0A  if an element in an MXN matrix is zeror, the entire row and column is set to 0.%0A   */%0A%0A  /**%0A   *%0A   * @param matrix%0A   * @return%0A   */%0A  public static int%5B%5D%5B%5D setToZero(int%5B%5D%5B%5D matrix)%7B%0A    List%3CHolder2D%3E markPoints = new ArrayList%3C%3E();%0A    /*%0A    First paas to find mark points%0A     */%0A    for(int i = 0; i %3C matrix.length ; i++)%7B%0A      for(int j  = 0; j %3C matrix%5Bi%5D.length ; j++)%7B%0A        if(matrix%5Bi%5D%5Bj%5D == 0)%7B%0A          Holder2D holder2D = new Holder2D();%0A          holder2D.i = i;%0A          holder2D.j = j;%0A          markPoints.add(holder2D);%0A        %7D%0A      %7D%0A    %7D%0A    /*%0A    Second passs to change all rows and columsn corresponding to markpoints to zero%0A     */%0A    for(Holder2D holder2D : markPoints)%7B%0A      int i = holder2D.i;%0A      int j = holder2D.j;%0A      paintWithZeros(matrix,i,j);%0A    %7D%0A    return matrix;%0A  %7D%0A%0A  public static void paintWithZeros(int%5B%5D%5B%5D matrix,int i,int j)%7B%0A    /*%0A    Paint row and co,un%0A     */%0A    for(int index = 0; index %3C matrix%5Bi%5D.length ; index++)%7B%0A      matrix%5Bi%5D%5Bindex%5D = 0;%0A    %7D%0A    /*%0A    Paint column%0A     */%0A    for(int index = 0;index %3C matrix.length ; index++)%7B%0A      matrix%5Bindex%5D%5Bj%5D = 0;%0A    %7D%0A  %7D%0A%0A  /**%0A   *%0A   * @param args%0A   */%0A  public static void main(String%5B%5D args) %7B%0A    int%5B%5D%5B%5D matrix = new int%5B%5D%5B%5D%7B%0A        %7B1,2,3%7D,%0A        %7B4,0,6%7D,%0A        %7B7,8,9%7D%0A    %7D;%0A    MatrixRotation.printMatrix(matrix);%0A    int%5B%5D%5B%5D modified = setToZero(matrix);%0A    MatrixRotation.printMatrix(modified);%0A    System.out.println(%22Next%22);%0A    matrix = new int%5B%5D%5B%5D%7B%0A        %7B1,2,3%7D,%0A        %7B4,5,6%7D,%0A        %7B7,8,0%7D%0A    %7D;%0A    MatrixRotation.printMatrix(matrix);%0A    modified = setToZero(matrix);%0A    MatrixRotation.printMatrix(modified);%0A  %7D%0A%0A%7D%0A

Set rows and columns of an mxn matrix to zero if any of the lements is zero.