Javascript Program for Maximum and Minimum in a square matrix. Last Updated : 20 Mar, 2023 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 = -5 Naive Method : We find maximum and minimum of matrix separately using linear search. Number of comparison needed is n2 for finding minimum and n2 for finding the maximum element. The total comparison is equal to 2n2. JavaScript // JavaScript Program for finding maximum // and minimum in a matrix function maxMin(arr, n){ let min = +2147483647; let max = -2147483648; // loop to find the maximum element for(let i = 0; i<n; i++){ for(let j = 0; j<n; j++){ if(max < arr[i][j]) max = arr[i][j]; } } // for finding the minimum element for(let i = 0; i<n; i++){ for(let j = 0; j<n; j++){ if(min > arr[i][j]) min = arr[i][j]; } } console.log("Maximum = " + max + ", Minimum = " + min); } // driver code for above approach let arr = [ [ 5, 9, 11 ], [ 25, 0, 14 ], [ 21, 6, 4 ] ]; maxMin(arr, 3); // THIS CODE IS CONTRIBUTED BY KIRTI AGARWAL(KIRTIAGARWAL23121999) OutputMaximum = 25, Minimum = 0 Time Complexity: O(N^2) Auxiliary Space: O(1) 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 extended form of method 3 of Maximum Minimum of Array. JavaScript <script> // Javascript program for finding maximum // and minimum in a matrix. let MAX = 100; // Finds maximum and minimum // in arr[0..n-1][0..n-1] // using pair wise comparisons function maxMin(arr,n) { let min = +2147483647; let max = -2147483648; // Traverses rows one by one for(let i = 0; i < n; i++) { for(let 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]; } } } document.write("Maximum = " + max + ", Minimum = " + min); } // Driver Code let arr = [ [ 5, 9, 11 ], [ 25, 0, 14 ], [ 21, 6, 4 ] ]; maxMin(arr, 3); // This code is contributed by sravan kumar </script> Output: Maximum = 25, Minimum = 0 Time Complexity: O(N^2) Auxiliary Space: O(1) Please refer complete article on Maximum and Minimum in a square matrix. for more details! Comment More infoAdvertise with us Next Article Javascript Program for Maximum and Minimum in a square matrix. kartik Follow Improve Article Tags : Matrix JavaScript Web Technologies DSA Practice Tags : Matrix Similar Reads 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 = -5 Naive Method : We find maximum and minimum of matrix 10 min read Javascript 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 : Approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside 2 min read Program to find the maximum element in a Matrix Given an NxM matrix. The task is to find the maximum element in this matrix. Examples: Input: mat[4][4] = {{1, 2, 3, 4}, {25, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; Output: 25 Input: mat[3][4] = {{9, 8, 7, 6}, {5, 4, 3, 2}, {1, 0, 12, 45}}; Output: 45 Approach: The idea is to traverse the mat 6 min read Javascript Program for Diagonally Dominant Matrix In mathematics, a square matrix is said to be diagonally dominant if for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row. More precisely, the matrix A is diagonally dominant 2 min read Maximum increase in value of Matrix to keep maximum rows and columns unchanged Given a matrix mat[][] of dimensionM*N, the task is to find the total maximum possible value must be increased to each cell(say mat[i][j]) such that maximum element of row i and column j remains unchanged. Examples: Input: N = 3, M = 3, mat[][] = { {4, 1, 3}, {3, 1, 1}, {2, 2, 0} } Output: 6 Explana 9 min read Find and remove maximum value in each row of a given Matrix Given a matrix mat[][] of size N * M, the task is to find and remove the maximum of each row from the matrix and add the largest among them and return the final sum. Perform these operations till the matrix becomes empty. Examples: Input: M = 3, N = 2, mat[][] = [[1, 2, 4], [3, 3, 1]]Output: 8Explan 5 min read Find all matrix elements which are minimum in their row and maximum in their column Given a matrix mat[][] of size M * N, the task is to find all matrix elements which are minimum in their respective row and maximum in their respective column. If no such element is present, print -1. Examples: Input: mat[][] = {{1, 10, 4}, {9, 3, 8}, {15, 16, 17}}Output: 15Explanation:15 is the onl 7 min read Find maximum of minimums from Layers of Matrix using numbers 1 to N^2 Given a square matrix of size N*N using numbers 1 to N^2, the task is to find the maximum of minimums of each layer of the matrix. The layers of the matrix are the boundary elements of the sub-matrix starting at (i, i) and ending at (N - i + 1, N - i + 1), where 1<= i<= ceil(N/2). Examples: In 7 min read Find row with maximum sum in a Matrix Given an N*N matrix. The task is to find the index of a row with the maximum sum. That is the row whose sum of elements is maximum. Examples: Input : mat[][] = { { 1, 2, 3, 4, 5 }, { 5, 3, 1, 4, 2 }, { 5, 6, 7, 8, 9 }, { 0, 6, 3, 4, 12 }, { 9, 7, 12, 4, 3 }, }; Output : Row 3 has max sum 35 Input : 11 min read Maximum sum of elements from each row in the matrix Given a matrix, find the maximum sum we can have by selecting just one element from every row. Condition is element selected from nth row must be strictly greater than element from (n-1)th row, else no element must be taken from row. Print the sum if possible else print -1. Examples : Input : 1 2 31 7 min read Like