kumar_ts
BAN USERpublic class MatrixZeroColumnRows {
public static void main(String[] args) {
int[][] matrix = { { 1, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, }, { 0, 1, 0, 0, 0, }, { 1, 0, 1, 1, 0, } };
printMatrix(matrix);
int[] result = zeroIntersection(matrix);
System.out.println(" row number : " + result[0]);
System.out.println("Column number : " + result[1]);
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(" " + matrix[i][j] + " ");
}
System.out.println();
}
}
public static int[] zeroIntersection(int[][] matrix) {
int[] arr = new int[2];
int row = 0;
int column = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] != 0) {
break;
} else {
row++;
}
}
if (row == matrix[0].length) {
arr[0] = i;
}
row = 0;
}
for (int j = 0; j < matrix[0].length; j++) {
for (int i = 0; i < matrix.length; i++) {
if (matrix[i][j] != 0) {
break;
} else {
column++;
}
}
if (column == matrix.length) {
arr[1] = j;
}
column = 0;
}
return arr;
}
}
Now once you get the row and column number , fill the matrix with 1 except matrix[1][4] =0 ;
- kumar_ts May 04, 2018