Check if a given matrix is sparse or not Last Updated : 20 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. Implementation: 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"; } Java // Java code to check // if a matrix is // sparse. import java.io.*; class GFG { static int MAX = 100; static boolean isSparse(int array[][], 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 public static void main(String args[]) { int array[][] = { { 1, 0, 3 }, { 0, 0, 4 }, { 6, 0, 0 } }; int m = 3, n = 3; if (isSparse(array, m, n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by // Nikita Tiwari. Python3 # Python 3 code to check # if a matrix is # sparse. MAX = 100 def isSparse(array,m, n) : counter = 0 # Count number of zeros # in the matrix for i in range(0,m) : for j in range(0,n) : if (array[i][j] == 0) : counter = counter + 1 return (counter > ((m * n) // 2)) # Driver Function array = [ [ 1, 0, 3 ], [ 0, 0, 4 ], [ 6, 0, 0 ] ] m = 3 n = 3 if (isSparse(array, m, n)) : print("Yes") else : print("No") # this code is contributed by # Nikita tiwari C# // C# code to check if a matrix is // sparse. using System; class GFG { static bool isSparse(int [,]array, 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 public static void Main() { int [,]array = { { 1, 0, 3 }, { 0, 0, 4 }, { 6, 0, 0 } }; int m = 3, n = 3; if (isSparse(array, m, n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by vt_m. PHP <?php // PHP code to check if a matrix is // sparse. $MAX = 100; function isSparse( $array, $m, $n) { $counter = 0; // Count number of zeros // in the matrix for ($i = 0; $i < $m; ++$i) for ($j = 0; $j < $n; ++$j) if ($array[$i][$j] == 0) ++$counter; return ($counter > (($m * $n) / 2)); } // Driver Code $array = array(array(1, 0, 3), array(0, 0, 4), array(6, 0, 0)); $m = 3; $n = 3; if (isSparse($array, $m, $n)) echo "Yes"; else echo "No"; // This code is contributed by anuj_67. ?> JavaScript <script> // Javascript code to check // if a matrix is // sparse. let MAX = 100; function isSparse(array, m, n) { let counter = 0; // Count number of zeros in the matrix for (let i = 0; i < m; ++i) for (let j = 0; j < n; ++j) if (array[i][j] == 0) ++counter; return (counter > parseInt((m * n) / 2), 10); } let array = [ [ 1, 0, 3 ], [ 0, 0, 4 ], [ 6, 0, 0 ] ]; let m = 3, n = 3; if (isSparse(array, m, n)) document.write("Yes"); else document.write("No"); </script> OutputYes Time Complexity: O(m*n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if a Matrix is Bitonic or not V Vineet Joshi Improve Article Tags : Misc Matrix DSA Practice Tags : MatrixMisc Similar Reads Check if a given number is sparse or not Write a function to check if a given number is Sparse or not.A number is said to be a sparse number if in the binary representation of the number no two or more consecutive bits are set.Example: Input: x = 72Output: trueExplanation: Binary representation of 72 is 01001000. There are no two consecuti 6 min read Check if a given matrix is Hankel or not Given a matrix m[][] of size n x n. The task is to check whether given matrix is Hankel Matrix or not.In linear algebra, a Hankel matrix (or catalecticant matrix), named after Hermann Hankel, is a square matrix in which each ascending skew-diagonal from left to right is constant. Examples: Input: n 7 min read Javascript Program to Check if a given matrix is sparse or not 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 elem 2 min read Check if a Matrix is Bitonic or not Given a matrix m[][], the task is to check if the given matrix is Bitonic or not. If the given matrix is Bitonic, then print YES. Otherwise, print NO. If all the rows and the columns of the given matrix have elements in one of the following orders: Strictly increasingStrictly decreasingStrictly incr 8 min read Check if a Matrix is Bitonic or not Given a matrix m[][], the task is to check if the given matrix is Bitonic or not. If the given matrix is Bitonic, then print YES. Otherwise, print NO. If all the rows and the columns of the given matrix have elements in one of the following orders: Strictly increasingStrictly decreasingStrictly incr 8 min read Check whether the given Matrix is balanced or not Given a matrix mat[][] of dimensions NxM, the task is to check whether the given matrix is balanced or not. Print âBalancedâ if it is a balanced matrix else print âUnbalancedâ. A matrix is balanced if all cells in the matrix are balanced and a cell of the matrix is balanced if the number of cells in 8 min read Like