C++ Program to Check if a given matrix is sparse or not Last Updated : 11 Jul, 2022 Comments Improve Suggest changes Like Article Like Report A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elements in the matrix, Examples: 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 a 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. C++ // CPP code to check if a matrix is // sparse. #include <iostream> using namespace std; const int MAX = 100; bool isSparse(int array[][MAX], int m, int n) { int counter = 0; // Count number of zeros in the matrix for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) if (array[i][j] == 0) ++counter; return (counter > ((m * n) / 2)); } // Driver Function int main() { int array[][MAX] = { { 1, 0, 3 }, { 0, 0, 4 }, { 6, 0, 0 } }; int m = 3, n = 3; if (isSparse(array, m, n)) cout << "Yes"; else cout << "No"; } Output: Yes Time complexity: O(m*n) where m and n are rows and columns respectively of a given matrix Auxiliary Space: O(1) Please refer complete article on Check if a given matrix is sparse or not for more details! Comment More infoAdvertise with us Next Article C++ Program to Check if a given matrix is sparse or not K kartik Follow Improve Article Tags : Matrix C++ Programs C++ DSA Practice Tags : CPPMatrix Similar Reads C++ Program to check if a matrix is symmetric A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the given matrix. Symmetric matrix can be obtain by changing row to column and column to row. Examples: Input : 1 2 3 2 1 4 3 4 3 Output : Yes Input : 3 5 8 3 4 7 8 5 3 Output : No A Simple solution is to do fo 3 min read C++ program to Convert a Matrix to Sparse Matrix Given a matrix with most of its elements as 0, convert this matrix to sparse matrix in C++Examples: Input: Matrix: 0 1 1 1 2 2 2 1 3 3 2 5 4 3 4 Output: Sparse Matrix: 0 1 0 0 0 0 2 0 0 3 0 0 0 0 5 0 0 0 0 4 Explanation: Here the Sparse matrix is represented in the form Row Column Value Hence the ro 3 min read C++ Program To Check if Two Matrices are Identical The below program checks if two square matrices of size 4*4 are identical or not. For any two matrices to be equal, the number of rows and columns in both the matrix should be equal and the corresponding elements should also be equal. Recommended: Please solve it on "PRACTICE" first, before moving 2 min read Check whether a Matrix is a Latin Square or not Given a square matrix of size N x N, the task is to check if it is Latin square or not. A square matrix is a Latin Square if each cell of the matrix contains one of N different values (in the range [1, N]), and no value is repeated within a row or a column. Examples: Input: 1 2 3 4 2 1 4 3 3 4 1 2 4 8 min read C++ Program to check idempotent matrix Given N * N matrix and the task is to check matrix is an idempotent matrix or not.Idempotent matrix: A matrix is said to be an idempotent matrix if the matrix multiplied by itself returns the same matrix. The matrix M is said to be an idempotent matrix if and only if M * M = M. In an idempotent matr 2 min read Like