PHP Program for Maximum and Minimum in a Square Matrix Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share 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 K kartik Follow Improve Article Tags : PHP Similar Reads 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 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 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 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 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