public class SparseMatrix {
public static void main(String[] args) {
int[][] matrix = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int count = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
count++;
}
}
}
if (count > (matrix.length * matrix[0].length) / 2) {
System.out.println("The given matrix is a sparse matrix.");
} else {
System.out.println("The given matrix is not a sparse matrix.");
}
}
}
The given matrix is a sparse matrix.matrix represented as a two-dimensional array. We initialize a variable count to 0.matrix. The outer loop iterates over the rows, and the inner loop iterates over the columns. For each element in the matrix, we check whether it is equal to 0. If it is equal to 0, we increment the count variable.matrix is greater than half the number of elements in the matrix. If it is greater than half the number of elements in the matrix, we print that the matrix is a sparse matrix. Otherwise, we print that the matrix is not a sparse matrix.