Reputation: 21
Given an integer matrix of size, say, M x N
, you have to write a program to remove all the rows and columns consisting of all zeros. Your program must remove those rows and columns that consist of zero valued elements only. That is, if all the elements in a row (column) are zeros, then remove that row (column). All the remaining rows and columns should be output.
Input Specification:
M
and N
. M
specifies the number of rows in the matrix and N
specifies the number of columns in the matrix.M
lines, each consisting of N
integers. Note: Assume 1 <= M <= 10
and 1 <= N <= 10
. Also, you may assume that there is at least one non-zero element in the given matrix.Output Specification:
Example1:
Sample Input:
4 4
1 2 3 4
0 0 0 0
5 6 7 8
0 0 0 3
Sample Output:
1 2 3 4
5 6 7 8
0 0 0 3
Example2:
Sample Input:
4 5
1 2 0 3 5
0 0 0 0 0
4 5 0 6 9
7 8 0 9 1
Sample Output:
1 2 3 5
4 5 6 9
7 8 9 1
Upvotes: 1
Views: 1463
Reputation:
Removing columns from a matrix is more difficult than removing rows. For this reason, to simplify the code, you can use the matrix transposition something like this:
public static void main(String[] args) {
int[][] arr1 = {
{1, 2, 3, 4},
{0, 0, 0, 0},
{5, 6, 7, 8},
{0, 0, 0, 3}};
int[][] arr2 = {
{1, 2, 0, 3, 5},
{0, 0, 0, 0, 0},
{4, 5, 0, 6, 9},
{7, 8, 0, 9, 1}};
int[][] arr3 = removeZeroRowsNColumns(arr1);
int[][] arr4 = removeZeroRowsNColumns(arr2);
// output
Arrays.stream(arr3).map(Arrays::toString).forEach(System.out::println);
//[1, 2, 3, 4]
//[5, 6, 7, 8]
//[0, 0, 0, 3]
Arrays.stream(arr4).map(Arrays::toString).forEach(System.out::println);
//[1, 2, 3, 5]
//[4, 5, 6, 9]
//[7, 8, 9, 1]
}
public static int[][] removeZeroRowsNColumns(int[][] arr) {
return removeZeroRows(removeZeroColumns(arr));
}
public static int[][] removeZeroColumns(int[][] arr) {
return transposeMatrix(removeZeroRows(transposeMatrix(arr)));
}
public static int[][] transposeMatrix(int[][] arr) {
// assume that we have a rectangular array, i.e. matrix
int[][] transposed = new int[arr[0].length][arr.length];
IntStream.range(0, arr.length)
.forEach(i -> IntStream.range(0, arr[0].length)
.forEach(j -> transposed[j][i] = arr[i][j]));
return transposed;
}
public static int[][] removeZeroRows(int[][] arr) {
return Arrays.stream(arr)
// filter out rows consisting of all zeros
.filter(row -> !Arrays.stream(row).allMatch(i -> i == 0))
.toArray(int[][]::new);
}
Upvotes: 0
Reputation: 21
The solution I found was as follows. It works only for rows and columns having either non-negative or non-positive numbers:
import java.util.Scanner;
public class RemoveZeroFromMatrix {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter rows and column");
int m = s.nextInt();
int n = s.nextInt();
int[][] a = new int[m][n];
int[][] b = new int[m][n];
int[][] c = new int[m][n];
System.out.println("enter the matrix");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println("input matrix");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int sumRow = 0, x = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
sumRow += a[i][j];
}
if (sumRow != 0) {
for (int k = 0; k < n; k++) {
b[i - x][k] = a[i][k];
}
} else {
x++;
}
sumRow = 0;
}
int sumCol = 0, y = 0, temp = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - x; j++) {
sumCol += b[j][i];
}
if (sumCol != 0) {
for (int k = 0; k < n - x; k++) {
if (temp == 0) {
c[k][i] = b[k][i];
}
if (temp == 1 && n > 3) {
c[k][i] = b[k][i + 1];
}
}
} else {
for (int k = 0; k < n - x; k++) {
c[k][i] = b[k][i + 1];
temp = 1;
}
}
sumCol = 0;
}
System.out.println("outut matix:");
for (int i = 0; i < m - x; i++) {
for (int j = 0; j < n - y - temp; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}
Upvotes: 1