C++ Program for Maximum and Minimum in a square matrix. Last Updated : 20 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. C++ // C++ program for finding maximum and minimum in // a matrix. #include<bits/stdc++.h> using namespace std; // Finds maximum and minimum in arr[0..n-1][0..n-1] // using pair wise comparisons void maxMin(int arr[3][3], int n) { int min = INT_MAX; int max = INT_MIN; // for finding the max element in given array 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 min element in given array for(int i = 0; i<n; i++){ for(int j = 0; j<n; j++){ if(min > arr[i][j]) min = arr[i][j]; } } cout << "Maximum = " << max << ", Minimum = " << min; } // Driver Program to test above function int main(){ int arr[3][3] = {{5, 9, 11} , {25, 0, 14} , {21, 6, 4}}; maxMin(arr, 3); return 0; } // 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. C++ // C++ program for finding maximum and minimum in // a matrix. #include<bits/stdc++.h> using namespace std; #define MAX 100 // Finds maximum and minimum in arr[0..n-1][0..n-1] // using pair wise comparisons void maxMin(int arr[][MAX], int n) { int min = INT_MAX; int max = INT_MIN; // 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]; } } } cout << "Maximum = " << max; << ", Minimum = " << min; } /* Driver program to test above function */ int main() { int arr[MAX][MAX] = {5, 9, 11, 25, 0, 14, 21, 6, 4}; maxMin(arr, 3); return 0; } Output: Maximum = 25, Minimum = 0 Time Complexity: O(n^2) Auxiliary Space: O(1) since no extra space has been taken. Please refer complete article on Maximum and Minimum in a square matrix. for more details! Comment More infoAdvertise with us Next Article C++ Program for Maximum and Minimum in a square matrix. kartik Follow Improve Article Tags : Matrix C++ Programs C++ DSA Practice Tags : CPPMatrix Similar Reads C++ Program to Find the Minimum and Maximum Element of an Array Given an array, write functions to find the minimum and maximum elements in it. Example: C++ // C++ program to find minimum (or maximum) element // in an array. #include <bits/stdc++.h> using namespace std; int getMin(int arr[], int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = 3 min read C++ Program To Find Normal and Trace of a Matrix Given a 2D matrix, the task is to find Trace and Normal of matrix.Normal of a matrix is defined as square root of sum of squares of matrix elements.Trace of a n x n square matrix is sum of diagonal elements.Examples : Input: mat[][] = {{7, 8, 9}, {6, 1, 2}, {5, 4, 3}}; Output: Normal = 16 Trace = 11 2 min read C++ 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 : The approach is very simple. The idea is to run the loop for no_of_rows. Check each 2 min read Maximum in a 2D matrix using Multi-threading in C++ Given a 2-D matrix, find the element having maximum value using multi-threading. Prerequisite : Multithreading Examples : Input : {{1, 5, 3, 6}, {22, 10, 4, 34}, {4, 45, 67, 3}, {69, 3, 23, 3}} Output :69 Input :{{1, 2, 3} {2, 4, 5}} Output :5 A matrix can be of very large size so when it comes to t 2 min read Maximum perimeter of a square in a 2D grid Given a matrix of integers mat[][] of size N * M. The task is to find the maximum perimeter of a square in the matrix. The perimeter of a square is defined as the sum of all the values lying on the sides of the square.Examples: Input: mat[][] = { {-3, -2, 7}, {-4, 6, 0}, {-4, 8, 2}} Output: 16 The m 15+ min read How to Find Minimum and Maximum Element of an Array Using STL in C++? Given an array of n elements, the task is to find the minimum and maximum element of array using STL in C++.ExamplesInput: arr[] = {1, 45, 54, 7, 76}Output: min = 1, max = 76Explanation: 1 is the smallest and 76 is the largest among all elements.Input: arr[] = {10, 7, 5, 4, 6}Output: min = 4, max = 3 min read How to Find the Minimum and Maximum Element of a Vector Using STL in C++? In this article, we will learn how to find the minimum and maximum element in vector in C++.The simplest method to find the minimum and maximum element in vector is by using min_element() and max_element(). Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; 2 min read C++ Program for Identity Matrix Introduction to Identity Matrix : The dictionary definition of an Identity Matrix is a square matrix in which all the elements of the principal or main diagonal are 1's and all other elements are zeros. In the below image, every matrix is an Identity Matrix. In linear algebra, this is sometimes call 3 min read C++ Program to Print matrix in zag-zag fashion Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: 1 2 3 4 5 6 7 8 9 Output: 1 2 4 7 5 3 6 8 9 Approach of C++ code The approach is simple. Just simply iterate over every diagonal elements one at a time and change the directio 3 min read C++ Program For Boundary Elements of a Matrix Printing Boundary Elements of a Matrix. Given a matrix of size n x m. Print the boundary elements of the matrix. Boundary elements are those elements which are not surrounded by elements on all four directions, i.e. elements in first row, first column, last row and last column. Examples: Input: 1 4 min read Like