A matrix is a two dimensional data objects having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix is 0 then we say that the matrix is sparse.
Consider a definition of Sparse where a matrix is considered sparse if number of 0s is more than half of the elements in matrix,
Input : 1 0 3 0 0 4 6 0 0 Output : Yes There are 5 zeros. This count is more than half of matrix size. Input : 1 2 3 0 7 8 5 0 7 Output: No
To check whether a matrix is sparse matrix we only need to check the total number of elements that are equal to zero. If this count is more than (m * n)/2, we return true.
This article is contributed by Vineet Joshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C++ program to Convert a Matrix to Sparse Matrix
- Sparse Matrix and its representations | Set 1 (Using Arrays and Linked Lists)
- Sparse Matrix and its representations | Set 2 (Using List of Lists and Dictionary of keys)
- Sparse Matrix Representations | Set 3 ( CSR )
- Check if it is possible to make the given matrix increasing matrix or not
- Program to check if a matrix is Binary matrix or not
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Check given matrix is magic square or not
- Check if a given matrix is Hankel or not
- Check whether the given Matrix is balanced or not
- Check if the Matrix follows the given constraints or not
- Check whether a given matrix is orthogonal or not
- C Program To Check whether Matrix is Skew Symmetric or not
- Program to check if matrix is singular or not
- Check whether a Matrix is a Latin Square or not
- Check if row-major order path of Matrix is palindrome or not
- Check if Matrix sum is prime or not
- Check if a Matrix is Bitonic or not
- Check if a Matrix is Reverse Bitonic or Not
- Check if two elements of a matrix are on the same diagonal or not
Improved By : vt_m