PHP Program to Check the Given Matrix is Sparse or Not Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share 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 : YesThere 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. 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 = [[1, 0, 3], [0, 0, 4], [6, 0, 0]]; $m = 3; $n = 3; if (isSparse($array, $m, $n)) { echo "Yes"; } else { echo "No"; } ?> OutputYesTime Complexity: O(n*m)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 PHP | ImagickKernel separate() Function K kartik Follow Improve Article Tags : PHP Similar Reads How to Check Whether a Variable is Empty in PHP? Given some values of variables, the task is to check whether the variable is empty or not in PHP. An empty variable can refer to a variable that has not been set, a variable that has been explicitly set to an empty value, or a variable that contains a value that is considered empty. In this article, 5 min read How to Find Multiplication of Two Matrices of any Size in PHP? Multiplying two matrices is a common operation in linear algebra. To multiply two matrices of any size in PHP, you can create a function that takes two matrices as input and returns their product. Below are the approaches to find the Multiplication of two Matrices of any size in PHP:Table of Content 4 min read How to Access and Print Matrix Element at Specific Position in PHP ? Accessing and printing elements at specific positions within a matrix is a fundamental operation in PHP when dealing with two-dimensional arrays. A matrix in PHP can be represented as an array of arrays, where each sub-array represents a row in the matrix. This article explores various approaches to 3 min read How to Calculate Determinant of a Matrix in PHP? Given a Matrix of size M*N, where N and M is an Integer, we need to Calculate calculate of a Matrix in PHP. Example: Input: [[1, 2],[2, 3]]Output: 6Explanation: (1*3)-(2*2)= 6-4= 2 There are several ways to find the determinant of the matrix in PHP which are as follows: Table of Content Using Recurs 4 min read PHP | ImagickKernel separate() Function The ImagickKernel::separate() function is an inbuilt function in PHP which is used to separate a linked set of kernels and returns an array of ImagickKernels. This function is used to count the kernels in an object or see the kernels of an object. Syntax: array ImagickKernel::separate( void ) Parame 2 min read How to Declare and Initialize 3x3 Matrix in PHP ? PHP, a widely used scripting language, is primarily known for its web development capabilities. However, it also offers a robust set of features to perform various other programming tasks, including the creation and manipulation of matrices. A matrix is a two-dimensional array consisting of rows and 3 min read Like