- Define the contract of the function.
- Code the 2-passes algorithm.

public static void setZeros(int[][] matrix) {

int[] row = new int[matrix.length];

int[] column = new int[matrix[0].length];

// Store the row and column index with value 0

for (int i = 0; i < matrix.length; i++) {

for (int j = 0; j < matrix[0].length;j++) {

if (matrix[i][j] == 0) {

row[i] = 1;

column[j] = 1;

}

}

}

// Set arr[i][j] to 0 if either row i or column j has a 0

for (int i = 0; i < matrix.length; i++) {

for (int j = 0; j < matrix[0].length; j++) {

if ((row[i] == 1 || column[j] == 1)) {

matrix[i][j] = 0;

}

}

}

}

**#1) Define the contract of the function**

Name the function

*setZeros()*to operate on a passed-in a 2-dimension int array, which will set all rows and columns of the matrix to zero if the element is 0.

public static void setZeros(int[][] matrix)

**#2) Generic case algorithm**

Use a 2-passes scanning approach.

The first pass scans through the entire int[][] matrix to see which row and column has 0. During the scan, on detecting element being 0, flag the corresponding rows/columns to indicate they have 0's. For storing the flags, introduce a int[] for flagging rows and another int[] for flagging columns.

int[] row = new int[matrix.length];

int[] column = new int[matrix[0].length];

The key concepts here are that

*int[][]*is used to store the matrix, and utilizes a row array (int[]) and a column array (int[]) to store flags.