Java Program for Maximum and Minimum in a square matrix. Last Updated : 22 Feb, 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. Java // Java program for finding maximum // and minimum in a matrix. class GFG { static void maxMin(int arr[][], int n){ int min = +2147483647; int max = -2147483648; // for finding the maximum element for(int i = 0; i<n; i++){ for(int j = 0; j<n; j++){ if(max < arr[i][j]){ max = arr[i][j]; } } } // for finding the minimum element for(int i = 0; i<n; i++){ for(int j = 0; j<n; j++){ if(min > arr[i][j]){ min = arr[i][j]; } } } System.out.print("Maximum = " + max + ", Minimum = "+min); } // Driver program public static void main (String[] args) { int 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 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. Java // Java program for finding maximum // and minimum in a matrix. class GFG { static final int MAX = 100; // Finds maximum and minimum // in arr[0..n-1][0..n-1] // using pair wise comparisons static void maxMin(int arr[][], int n) { int min = +2147483647; int max = -2147483648; // Traverses rows one by one for (int i = 0; i < n; i++) { for (int 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]; } } } System.out.print("Maximum = "+max+ ", Minimum = "+min); } // Driver program public static void main (String[] args) { int arr[][] = {{5, 9, 11}, {25, 0, 14}, {21, 6, 4}}; maxMin(arr, 3); } } // This code is contributed by Anant Agarwal. Output: Maximum = 25, Minimum = 0 Time complexity: O(n^2) for given n*n matrix Auxiliary space: O(1) because constant space is being used Please refer complete article on Maximum and Minimum in a square matrix. for more details! Comment More infoAdvertise with us Next Article Java Program for Maximum and Minimum in a square matrix. kartik Follow Improve Article Tags : Java Matrix Java Programs DSA Practice Tags : JavaMatrix Similar Reads Java Program to Find the Maximum Element in a Matrix Given a multi-dimensional 2D matrix of n rows and m column order N Ã M. The task is to find the maximum element in the given matrix. Illustration: Input : mat[][] = { {1,3,4,19}, {11,10,12,1}, {7,9,0,4,99} } Output : 99 Methods: Iterative method (naive approach)Using the principle of recursion (Bit 3 min read Java 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 : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each ele 2 min read Finding the Minimum or Maximum Value in Java ArrayList The minimum value is the one with the smallest value and the maximum value is the one with the largest value. The main task here is to find the minimum and maximum value from the ArrayList. Consider an example of an ArrayList, and we need to find the largest and the smallest element. Example: Input 5 min read How to Find the Minimum or Maximum Element from the Vector in Java? The Collection is a framework offered by Java that provides an architecture to store a group of objects. One such collection is Vector(). There are many ways through which we can find the minimum and maximum elements in a Vector. These methods have been discussed below: Methods: Using Collection.min 3 min read Java 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 3 min read Java Program to Maximize sum of diagonal of a matrix by rotating all rows or all columns Given a square matrix, mat[][] of dimensions N * N, the task is find the maximum sum of diagonal elements possible from the given matrix by rotating either all the rows or all the columns of the matrix by a positive integer. Examples: Input: mat[][] = { { 1, 1, 2 }, { 2, 1, 2 }, { 1, 2, 2 } }Output: 3 min read Java Program to Display Lower Triangular Matrix Lower Triangular Matrix is a square matrix in which all the elements above the principal diagonal are 0. If the matrix is not a square matrix, it can never be called the lower triangular matrix. Examples Input 1: mat[][] = { {2, 1, 4}, {1, 2, 3}, {3, 6, 2}} Output : mat[][]= {{2, 0, 0}, {1, 2, 0}, { 2 min read Find smallest and largest element from square matrix diagonals Given a square matrix of order n*n, find the smallest and largest elements from both diagonals of the given matrix. Examples: Input : matrix = { {1, 2, 3, 4, -10}, {5, 6, 7, 8, 6}, {1, 2, 11, 3, 4}, {5, 6, 70, 5, 8}, {4, 9, 7, 1, 5}}; Output : Principal Diagonal Smallest Element: 1 Principal Diagona 15+ min read How to Find the Maximum Element in an Array? In Java, the array is a data structure that allows the users to store data of the same type in contiguous memory locations. To find the maximum element in an Array in Java, we can sort the array in ascending order using the Arrays.sort() method and then we can access the last element of the array wh 1 min read Javascript 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 = -5 Naive Method : We find maximum and minimum of matrix 3 min read Like