PHP Program for Maximum and Minimum in a Square Matrix Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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 minimum of a matrix separately using linear search. Number of comparisons needed is n2 for finding the minimum and n2 for finding the maximum element. The total comparison is equal to 2n2.Pair Comparison (Efficient method)Select two elements from the matrix one from the start of a row of the matrix another from the end of the same row of the matrix, compare them and next compare smaller of them to the minimum of the matrix and larger of them to the maximum of the matrix. We can see that for two elements we need 3 compare so for traversing whole of the matrix we need total of 3/2 n2 comparisons.Note: This is the extended form of method 3 of Maximum Minimum of Array. PHP <?php // PHP program for finding // maximum and minimum in // a matrix. $MAX = 100; // Finds maximum and minimum // in arr[0..n-1][0..n-1] // using pair wise comparisons function maxMin($arr, $n) { $min = PHP_INT_MAX; $max = PHP_INT_MIN; // Traverses rows one by one for ($i = 0; $i < $n; $i++) { for ($j = 0; $j <= $n / 2; $j++) { // Compare elements from beginning // and end of current row if ($arr[$i][$j] > $arr[$i][$n - $j - 1]) { if ($min > $arr[$i][$n - $j - 1]) $min = $arr[$i][$n - $j - 1]; if ($max< $arr[$i][$j]) $max = $arr[$i][$j]; } else { if ($min > $arr[$i][$j]) $min = $arr[$i][$j]; if ($max < $arr[$i][$n - $j - 1]) $max = $arr[$i][$n - $j - 1]; } } } echo "Maximum = " , $max ,", Minimum = " , $min; } // Driver Code $arr = array( array(5, 9, 11), array(25, 0, 14), array(21, 6, 4) ); maxMin($arr, 3); ?> OutputMaximum = 25, Minimum = 0Please refer complete article on Maximum and Minimum in a square matrix for more details! Comment More infoAdvertise with us Next Article PHP Program for Maximum and Minimum in a Square Matrix kartik Follow Improve Article Tags : Matrix Web Technologies PHP PHP Programs DSA +1 More Practice Tags : Matrix Similar Reads PHP Program to Find maximum element of each row in a matrix 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 in 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 for Minimum Product Subset of an Array Given an array Arr, we have to find the minimum product possible with the subset of elements present in the array. The minimum product can be a single element also.Examples: Input : Arr = [ -1, -1, -2, 4, 3 ] Output : -24 Explanation : Minimum product will be ( -2 * -1 * -1 * 4 * 3 ) = -24 Input : A 3 min read PHP Program for Frequencies of Even and Odd Numbers in a Matrix Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix.Examples: Input : m = 3, n = 3 [[ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]Output : Frequency of odd number = 5 Frequency of even number = 4Input : m = 3, n = 3 [[ 10, 11, 12 ], [ 13, 14, 15 ], [ 16, 17, 2 min read PHP Program for Number of pairs with maximum sum Write a PHP program for a given array arr[], count the number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j.Example:Input : arr[] = {1, 1, 1, 2, 2, 2} Output: 3 Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the 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 PHP Program for Maximum Difference Between Groups of Size Two Given an array of even number of elements, form groups of 2 using these array elements such that the difference between the group with highest sum and the one with lowest sum is maximum.Note: An element can be a part of one group only and it has to be a part of at least 1 group. Examples: Input : Ar 3 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 Find the Size of Subarray With Maximum Sum Given an Array, the task is to find the size of the subarray that yields the maximum sum. This article provides a comprehensive guide on how to implement a PHP program to solve this problem efficiently.Examples: Input: Arr = [ 1, -2, 1, 1, -2, 1 ]Output: Length of the subarray is 2Explanation: The s 2 min read PHP Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. Example: Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input: 1 2 3 4 5 6 7 8 9 10 11 3 min read Like