Python Program for Maximum and Minimum in a square matrix.
Last Updated :
21 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.
Python3
# Python program for finding the
# Maximum and Minimum in a matrix
# function print the MAX and MIN value
def MAXMIN(arr, n):
MAX = -10**9
MIN = 10**9
# finding the MAXimum element
for i in range(n):
for j in range(n):
if(MAX < arr[i][j]):
MAX = arr[i][j]
# finding the MINimum element
for i in range(n):
for j in range(n):
if(MIN > arr[i][j]):
MIN = arr[i][j]
print("Maximum = ", MAX, " Minimum = ", MIN)
# driver code to test above function
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.
Python3
# Python3 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
def MAXMIN(arr, n):
MIN = 10**9
MAX = -10**9
# Traverses rows one by one
for i in range(n):
for j in range(n // 2 + 1):
# 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]
print("MAXimum =", MAX, ", MINimum =", MIN)
# Driver Code
arr = [[5, 9, 11],
[25, 0, 14],
[21, 6, 4]]
MAXMIN(arr, 3)
# This code is contributed by Mohit Kumar
Output:
Maximum = 25, Minimum = 0
Please refer complete article on Maximum and Minimum in a square matrix. for more details!
Similar Reads
Python Program for Maximum size square sub-matrix with all 1s Write a Python 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
4 min read
Maximum and Minimum element in a Set in Python We are given a set and our task is to find the maximum and minimum elements from the given set. For example, if we have a = {3, 1, 7, 5, 9}, the maximum element should be 9, and the minimum element should be 1.Letâs explore different methods to do it:1. Using min() & max() functionThe built-in m
3 min read
Find the maximum and minimum element in a NumPy array An array can be considered as a container with the same types of elements. Python has its array module named array. We can simply import the module and create our array. But this module has some of its drawbacks. The main disadvantage is we can't create a multidimensional array. And the data type mu
4 min read
Return the maximum of an array or maximum ignoring any NaNs in Python In this article, we will cover how to return the maximum of an array or maximum by ignoring any NaNs in Python using NumPy. ExampleInput: [ -1. -2. nan 1000.] Output: 1000.0 Explanation: maximum value of the array ignoring nans. One approach to use the built-in Python function max(), along with the
3 min read
Python | Max/Min value in Nth Column in Matrix Sometimes, while working with Python Matrix, we may have a problem in which we require to find the minimum and maximum value of a particular column. This can have a possible application in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed
7 min read
Use of min() and max() in Python Prerequisite: min() max() in Python Let's see some interesting facts about min() and max() function. These functions are used to compute the maximum and minimum of the values as passed in its argument. or it gives the lexicographically largest value and lexicographically smallest value respectively,
2 min read