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 Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read