PHP Program to Check the Given Matrix is Sparse or Not Last Updated : 22 Jul, 2024 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 : 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 Program to Check the Given Matrix is Sparse or Not kartik Follow Improve Article Tags : Matrix Web Technologies PHP PHP Programs DSA +1 More Practice Tags : Matrix Similar Reads PHP Program to Check if a Matrix is Symmetric A square matrix is said to be a symmetric matrix if the transpose of the matrix is the same as the given matrix. A 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 ], [ 2 min read PHP Program to Check Involutory Matrix Given a matrix, the task is to check matrix is involutory matrix or not. Involutory Matrix: A matrix is said to be involutory matrix if the matrix multiply by itself return the identity matrix. Involutory matrix is the matrix that is its own inverse. The matrix A is said to be involutory matrix if A 2 min read PHP Program to Check if Matrix is Lower Triangular Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero.  Examples:  Input : mat = [[ 1, 0, 0, 0 ], [ 1, 4, 0, 0 ], [ 4, 6, 2, 0 ], [ 0, 4, 7, 6 ]];Output : Matrix is 2 min read PHP Program to Print matrix in snake pattern Given an n x n matrix in the given matrix, you have to print the elements of the matrix in the snake pattern.Examples: Input: mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output: 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input: mat[][] = { {1, 2, 3}, {4, 2 min read PHP Program to Find Sum of All Matrix Elements Finding the sum of all elements in a matrix is a common operation in mathematical computations and programming. In PHP, this can be achieved using various approaches, including loops and array functions. In this article, we will explore different methods to calculate the sum of all elements in a mat 4 min read PHP Program to Check if all rows of a matrix are circular rotations of each other Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not. Examples: Input: mat[][] = 1, 2, 3 3, 1, 2 2, 3, 1Output: Yes ,All rows are rotated permutation of each other.Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2Output: No, Explanation : As 3, 2, 1 i 2 min read PHP Program to Check for Upper Triangular Matrix Given a Square Matrix, the task is to check whether the matrix is in upper triangular form or not. A square matrix is called upper triangular matrix if all the entries below the main diagonal are zero. Examples:  Input: mat = [ [1, 3, 5, 3], [0, 4, 6, 2], [0, 0, 2, 5], [0, 0, 0, 6]];Output: Matrix i 2 min read PHP Program to Count Sets of 1s and 0s in a Binary Matrix Given a N x M Binary Matrix, count the number of sets where a set can be formed one or more same values in a row or column. Examples: Input: [[ 1, 0, 1 ], [ 0, 1, 0 ]] Output: 8 Explanation: There are six one-element sets (three 1s and three 0s). There are two two - element sets, the first one consi 2 min read PHP Program to Check horizontal and vertical symmetry in binary matrix Given a 2D binary matrix of N rows and M columns. The task is to check whether the matrix is horizontal symmetric, vertical symmetric, or both. The matrix is said to be horizontal symmetric if the first row is the same as the last row, the second row is the same as the second last row, and so on. An 3 min read PHP Program for Identity Matrix Introduction to Identity Matrix : The dictionary definition of an Identity Matrix is a square matrix in which all the elements of the principal or main diagonal are 1's and all other elements are zeros. In the below image, every matrix is an Identity Matrix. In linear algebra, this is sometimes call 4 min read Like