
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Given Matrix is Sparse or Not in JavaScript
To check if a given matrix is sparse or not, we will be discussing two different approaches, their complexities, and example codes. A sparse matrix is a special type of matrix in which the number of zeroes is strictly more than half of the the total number of elements present in the given matrix.
In this article we are having a 2D matrix, our task is to write a JavaScript program to check if a given matrix is sparse or not. Users must be familiar with conditional statement, nested for loop and javascript methods.
Example
Input: Matrix: [[1, 0, 0], [0, 3, 4], [8, 0, 0]] Total number of zeroes: 5 Total number of non - zeroes: 4 Total number of zeroes > total number of non-zeroes Output: true, sparse matrix
Approaches to Check Sparse Matrix
Here is a list of approaches to write a JavaScript program to check if a given matrix is sparse or not which we will be discussing in this article with stepwise explanation and complete example codes.
Using Brute Force
To check if a given matrix is sparse or not, we have used brute force approach where we have used nested for loop to iterate over each element of the matrix and check for element having value 0.
- We have declared a 2D matrix and defined a function count() that accepts matrix as argument.
- Initially we have set the count value of count to 0. The length property is used to get the number of rows and columns.
- We will use the nested for loop to traverse over the matrix and count variable to store the count of number of zeroes.
- We have used if/else statement to check elements of matrix. If the value is 0, we increase the counter.
- After the completion of loop, we have returned the count variable. Then the function count() is called and stored in variable zeroes.
- Then we have used if/else statement to compare the matrix size and zeroes. If zeroes is greater than half of the matrix size then matrix is sparse, if size is greater than zeroes then matrix is not sparse.
Example
Here is a complete example code implementing above mentioned steps to check if a given matrix is sparse or not using brute force approach.
let matrix = [ [1, 0, 0], [0, 3, 4], [8, 0, 0] ]; console.log("The given matrixrix is: ") console.log(matrix); function count(matrix) { let m = matrix.length; let n = matrix[0].length; let count = 0; for (let i = 0; i size / 2) { console.log("The given matrixrix is a sparse matrixrix") } else { console.log("The given matrixrix is not a sparse matrixrix") }
Using filter() Method
In this approach to check if a given matrix is sparse or not, we have used filter() method. First we have flattened the 2D matrix into 1D array and then applied filter() method on it.
- We have declared a 2D matrix and defined a function count() that accepts matrix as argument.
- Then we have used matrix.flat() method which flattens the 2D array into single dimension array. We have stored this single dimension array in arr.
- Then we have used filter() method which filters out element having value 0 from single dimension array. Then we have used length property to get the count of all elements having value 0 and store it in count.
- Then we have compared the count variable with half of the array size. If zeroes count is more than half of the matrix size then matrix is sparse.
Example
Here is a complete example code implementing above mentioned steps to check if a given matrix is sparse or not using filter() method.
let matrix = [ [1, 0, 0], [0, 3, 4], [8, 0, 0] ]; console.log(matrix); function isSparse(matrix) { const arr = matrix.flat(); const count = arr.filter(x => x === 0).length; return count > arr.length / 2; } if(isSparse(matrix)) { console.log("The given matrix is a sparse matrix"); } else { console.log("The given matrix is not a sparse matrix"); }
Complexity Comparison
Here is a comparison of time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Brute Force | O(m * n) | O(1) |
filter() Method | O(m * n) | O(m * n) |
Conclusion
In this article to write a JavaScript program to check if a given matrix is sparse or not we have used two different approaches. These approaches are: by using brute force approach and by using filter() method. Out of the above two approaches, brute force approach is the efficient approach.
Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.