# JavaScript program to check if a given matrix is sparse or not

In mathematics, a matrix is a set of integers or numbers stored in a rectangular form which is equivalent to the 2D array in programming or JavaScript programming. A sparse matrix is a special type of matrix in which the number of zeros is strictly more than the total number of elements or numbers present in the given matrix. We will be given a matrix and we have to find whether the current matrix is a sparse matrix or not.

### Input

Mat = [ [1, 0, 0],
[0, 3, 4],
[8, 0, 0]]


### Output

Yes, the given matrix is a sparse matrix.


### Explanation

We have a total of 5 zeroes present in the above matrix and the total number of integers in the given matrix is 9, which means the current matrix is a sparse matrix.

### Input

Mat = [ [1, 2, 3, 4],
[0, 0, 0, 0],
[5, 6, 7, 8],
[0, 0, 0, 0]]


### Output

No, the given matrix is not a sparse matrix.


### Explanation

We have a total of 16 elements present in the given matrix and a total of 8 zeroes are present. As per the definition, we need the majority of elements as zero which means strictly more than half elements must be zero.

## Approach

We have seen the examples to understand the problem, now let us move to the steps that we are going to follow to implement the code −

• First, we will create a function that will take the given matrix as the parameter and returns the number of zeros present in the matrix.

• We will use the nested for loops to traverse over the matrix and a variable to store the count of number of zeros.

• We will call to the function and store the return value in a variable and check.

• If the number of zeros are less or equal as compare to the half number of total variables, then will print it is not a sparse matrix.

• Otherwise, we will print given matrix is a sparse matrix.

### Example

// function to count number of zeros present in the given matrix
function countZeros(mat){

// getting the number of rows present in the given matrix.
var n = mat.length;

// getting the number of columns present in the given matrix.
var m = mat.length;
var count = 0; // variable to count number of zeros

// traversing over the given matrix
for(var i = 0;i < n; i++){
for(var j = 0; j<m; j++){
// if current element is zero increase the count
if(mat[i][j] == 0){
count++;
}
}
}

// returing true as all the values matched
return count;
}

// defining the matrix
var mat = [ [1, 0, 0],
[0, 3, 4],
[8, 0, 0]]
console.log("The given matrix is: ")
console.log(mat);

// counting number of zeros in the given matrix
var zeros = countZeros(mat);
var size = (mat.length)*(mat[0].length);
if(zeros > size/2){
console.log("The given matrix is a sparse matrix")
}
else{
console.log("The given matrix is not a sparse matrix")
}


### Time and Space Complexity

The time complexity of the above code is O(N*M) where N is the number of rows of the given matrix and M is the number of columns in the given matrix.

The space complexity of the above code is O(1), as we are not using any extra space to store the matrix.

## Conclusion

In this tutorial, we have implemented a JavaScript program to check if the given matrix is a sparse matrix or not. A sparse matrix is a special type of matrix in which the number of zeros is strictly more than the total number of elements or numbers present in the given matrix. We have implemented code in O(N*M) time complexity and work in the O(1) space complexity.

Updated on: 20-Apr-2023

61 Views