PHP Program to Find maximum element of each row in a matrix Last Updated : 22 Jul, 2024 Comments Improve Suggest changes 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 maximum element of each row in a matrix kartik Follow Improve Article Tags : Searching Matrix Technical Scripter Web Technologies PHP PHP Programs DSA +3 More Practice Tags : MatrixSearching Similar Reads 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 Interchange elements of first and last rows in matrix Given a 4 x 4 matrix, we have to interchange the elements of first and last row and show the resulting matrix.Examples : Input : 3 4 5 0 2 6 1 2 2 7 1 2 2 1 1 2Output : 2 1 1 2 2 6 1 2 2 7 1 2 3 4 5 0Input : 9 7 5 1 2 3 4 1 5 6 6 5 1 2 3 1Output : 1 2 3 1 2 3 4 1 5 6 6 5 9 7 5 1The approach is very 2 min read PHP Program for Maximum and Minimum in a Square Matrix Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : Arr = [ [ 5, 4, 9 ], [ 2, 0, 6 ], [ 3, 1, 8 ] ]; Output : Maximum = 9, Minimum = 0 Input : Arr = [[ -5, 3 ], [ 2, 4 ]]; Output : Maximum = 4, Minimum = -5Naive MethodWe find the maximum and mini 2 min read PHP Program for Maximum sum of i*arr[i] among all rotations of a given array Given an array arr[] of n integers, find the maximum that maximizes the sum of the value of i*arr[i] where i varies from 0 to n-1.Examples: Input: arr[] = {8, 3, 1, 2} Output: 29 Explanation: Lets look at all the rotations, {8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11 {3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 8 7 min read PHP Program for Maximum Equilibrium Sum in an Array Given an array arr[]. Find the maximum value of prefix sum which is also suffix sum for index i in arr[].Examples: Input : arr[] = {-1, 2, 3, 0, 3, 2, -1}Output : 4Prefix sum of arr[0..3] = Suffix sum of arr[3..6]Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2}Output : 7Prefix sum of arr[0..3] = Suffix su 3 min read PHP Program for Number of local extrema in an array You are given an array on n-elements. An extrema is an element that is either greater than its both of neighbors or less than its both neighbors. You have to calculate the number of local extrema in given array. Note: 1st and last elements are not extrema.Examples : Input : a[] = {1, 5, 2, 5}Output 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 How to Find the Largest Element in an Array in PHP? Given an array containing some elements, the task is to find the largest element from an array in PHP. PHP provides several ways to achieve this, each with its own advantages and use cases. Below are the approaches to find the largest element in an array in PHP:Table of ContentUsing max() FunctionUs 4 min read PHP Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example : Input: arr[] = {1, 14, 2, 16, 10, 20}Output: The third Largest element is 14Explanation: Largest element is 20, second largest element is 16 and third largest element is 14Inp 5 min read PHP Program to Find Maximum Value of Sum ( i*arr[i]) with Only Rotations on Given Array Allowed Given an array, only rotation operation is allowed on array. We can rotate the array as many times as we want. Return the maximum possible summation of i*arr[i].Examples: Input: Arr = [ 1, 20, 2, 10 ] Output: 72 We can get 72 by rotating array twice. {2, 10, 1, 20} 20*3 + 1*2 + 10*1 + 2*0 = 72 Input 4 min read Like