PHP Program to Find maximum element of each row in a matrix Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a matrix, the task is to find the maximum element of each row.Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21]Output :3976Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]Output :216556Approach : The approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside the row and find for the maximum element. Finally, print the element. Below is the implementation of the above approach: PHP <?php // PHP program to find maximum // element of each row in a matrix $N = 4; // Print array element function printArray($result, $no_of_rows) { for ($i = 0; $i < $no_of_rows; $i++) { echo $result[$i]." "; } } // Function to get max element function maxelement($no_of_rows, $arr) { global $N; $i = 0; // Initialize max to 0 at beginning // of finding max element of each row $max = 0; $result=array_fill(0,$no_of_rows,0); while ($i < $no_of_rows) { for ($j = 0; $j < $N; $j++) { if ($arr[$i][$j] > $max) { $max = $arr[$i][$j]; } } $result[$i] = $max; $max = 0; $i++; } printArray($result,$no_of_rows); } // Driver code $arr = array(array(3, 4, 1, 8), array(1, 4, 9, 11), array(76, 34, 21, 1), array(2, 1, 4, 5)); // Calling the function maxelement(4, $arr); // This code is contributed by mits ?> Output8 11 76 5 Complexity Analysis:Time Complexity: O(N * M) where N is the number of rows and M is the number of columns in the given matrix.Space Complexity: O(N) where N is the number of rows in the given matrix.Please refer complete article on Find maximum element of each row in a matrix for more details! Comment More infoAdvertise with us Next Article PHP Program to Find the Maximum Element in a Matrix K kartik Follow Improve Article Tags : PHP Similar Reads PHP Program to Find the Maximum Element in a Matrix Given a matrix, the task is to find the maximum element of the matrix (arrays of arrays) in PHP. Finding the maximum element in a matrix is a common task that involves iterating through all the elements of the matrix and keeping track of the largest value found. Example: Input: matrix = [ [1, 2, 3], 4 min read PHP program to find the maximum and the minimum in array Finding the maximum and minimum in an array involves determining the largest and smallest values within a given set of numbers. This task is crucial for analyzing data, identifying trends, or filtering outliers. Various methods, from simple traversal to optimized algorithms, can achieve this.Example 7 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 Find the Second Largest Element in an Array in PHP We will be given with an integer array, i.e. $array1 or $ array2, and the task is to find the second largest element in the array. There are multiple ways to approach and solve this problem however using sorting is the most common and concise approach as we use the rsort() function for further opera 3 min read Find the Most Frequent Element in an Array in PHP Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1, 2 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 Like