Program to determine whether a given matrix is a sparse matrixExplanationIn this program, we need to check whether the given matrix is the sparse matrix. Sparse MatrixA matrix is said to be sparse matrix if most of the elements of that matrix are 0. It implies that it contains very less non-zero elements. To check whether the given matrix is the sparse matrix or not, we first count the number of zero elements present in the matrix. Then calculate the size of the matrix. For the matrix to be sparse, count of zero elements present in an array must be greater than size/2. Number of zeroes present in above matrix is 6 and size of the matrix is 3 * 3 = 9. Since, 6 > 4.5 that means, most elements of given array are zero. Hence, the above matrix is a sparse matrix. Algorithm
SolutionPythonOutput: Given matrix is a sparse matrix COutput: Given matrix is a sparse matrix JAVAOutput: Given matrix is a sparse matrix C#Output: Given matrix is a sparse matrix PHPOutput: Given matrix is a sparse matrix Next Topic# |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India