Java Program for Maximum and Minimum in a square matrix.
Last Updated :
22 Feb, 2023
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!
Similar Reads
Java Program for Maximum sum rectangle in a 2D matrix | DP-27 Write a Java program for a given 2D array, the task is to find the maximum sum subarray in it. For example, in the following 2D array, the maximum sum subarray is highlighted with blue rectangle and sum of this subarray is 29. This problem is mainly an extension of the Largest Sum Contiguous Subarra
5 min read
Java Program for Maximum size square sub-matrix with all 1s Write a Java program for a given binary matrix, the task is to find out the maximum size square sub-matrix with all 1s. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Approach: Let the given binary matrix be M[R][C]. The idea of the algorithm is to construct an au
5 min read
Finding minimum and maximum element of a Collection in Java A Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit. These are: Finding minimum and maximum element of a Collection can be easily done using the Co
6 min read
Min and Max in a List in Java Given an unsorted list of integers, find maximum and minimum values in it. Input : list = [10, 4, 3, 2, 1, 20] Output : max = 20, min = 1 Input : list = [10, 400, 3, 2, 1, -1] Output : max = 400, min = -1Sorting This is least efficient approach but will get the work done. The idea is to sort the lis
5 min read
Find max or min value in an array of primitives using Java A simple method is traverse the array. We initialize min and max as first elements and then compare every element (Starting from 2nd) with current min and max.Java// Java program to find min & max in int[] // array without asList() public class MinNMax { public static void main(String[] args) {
3 min read