Check if a given matrix is sparse or not - GeeksforGeeks

25 May, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 Functionint 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 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.
# 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 Functionarray = [ [ 1, 0, 3 ],          [ 0, 0, 4 ],          [ 6, 0, 0 ] ]m = 3n = 3 if (isSparse(array, m, n)) :    print("Yes")else :    print("No")          # this code is contributed by# Nikita tiwari
// 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 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.?>
<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>
Output:  
Yes
This article is contributed by Vineet Joshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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. 
vt_m
suresh07
Matrix
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum size square sub-matrix with all 1s
Maximum size rectangle binary sub-matrix with all 1s
Min Cost Path | DP-6
Printing all solutions in N-Queen Problem
Rotate a matrix by 90 degree in clockwise direction without using any extra space
The Celebrity Problem
Python program to multiply two matrices
Search in a row wise and column wise sorted matrix
Efficiently compute sums of diagonals of a matrix
Real-time application of Data Structures