
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find smallest and largest element from square matrix diagonals in C++
In this problem, we are given a square matrix of size nXn. Our task is to Find smallest and largest element from square matrix diagonals. We need to find the smallest and largest elements of the primary and secondary diagonals of the matrox.
Let’s take an example to understand the problem,
Input
mat[][] = { {3, 4, 7}, {5, 2, 1}, {1, 8, 6} }
Output
Smallest element in Primary Diagonal = 2 Largest element in Primary Diagonal = 6 Smallest element in Secondary Diagonal = 1 Largest element in Secondary Diagonal = 7
Solution Approach
A simple solution to solve the problem is using nested loops. For checking elements at primary diagonal we will consider i = j. And for the secondary diagonal, we will consider i + j = n -1. We will find the maximum and minimum elements of the matrix for both primary and secondary diagonal.
Program to illustrate the working of our solution,
Example
#include<iostream> using namespace std; void findMaxAndMinOfDiagonals(int mat[3][3], int n){ if (n == 0) return; int pDiagMin = mat[0][0], pDiagMax = mat[0][0]; int sDiagMin = mat[0][n - 1 ], sDiagMax = mat[0][n - 1]; for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { if (i == j){ if (mat[i][j] < pDiagMin) pDiagMin = mat[i][j]; if (mat[i][j] > pDiagMax) pDiagMax = mat[i][j]; } if ((i + j) == (n - 1)) { if (mat[i][j] < sDiagMin){ sDiagMin = mat[i][j]; } if (mat[i][j] > sDiagMax) sDiagMax = mat[i][j]; } } } cout<<("\nSmallest Element of Principal Diagonal : ")<<pDiagMin; cout<<("\nGreatest Element of Principal Diagonal : ")<<pDiagMax; cout<<("\nSmallest Element of Secondary Diagonal : ")<<sDiagMin; cout<<("\nGreatest Element of Secondary Diagonal : ")<<sDiagMax; } int main(){ int mat[3][3] = { { 3, 4, 7 }, { 0, 2, 1 }, { 1, 7, 8 } }; int n = sizeof(mat) / sizeof(mat[0]); findMaxAndMinOfDiagonals(mat, n); }
Output
Smallest Element of Principal Diagonal : 2 Greatest Element of Principal Diagonal : 8 Smallest Element of Secondary Diagonal : 2 Greatest Element of Secondary Diagonal : 7
Another more effective solution is reducing the nested loop to single loop using the fact that for the primary diagonal both the indices of the mat are the same i.e.
primary diagonal elements = mat[i][j]. Similarly, for secondary diagonal elements = mat[i][n - i - 1]
Program to illustrate the working of our solution,
Example
#include<iostream> using namespace std; void findMaxAndMinOfDiagonals(int mat[3][3], int n){ if (n == 0) return; int pDiagMin = mat[0][0], pDiagMax = mat[0][0]; int sDiagMin = mat[0][n - 1 ], sDiagMax = mat[0][n - 1]; for (int i = 1; i < n; i++) { if (mat[i][i] < pDiagMin) pDiagMin = mat[i][i]; if (mat[i][i] > pDiagMax) pDiagMax = mat[i][i]; if (mat[i][n - 1 - i] < sDiagMin) sDiagMin = mat[i][n - 1 - i]; if (mat[i][n - 1 - i] > sDiagMax) sDiagMax = mat[i][n - 1 - i]; } cout<<("\nSmallest Element of Principal Diagonal : ")<<pDiagMin; cout<<("\nGreatest Element of Principal Diagonal : ")<<pDiagMax; cout<<("\nSmallest Element of Secondary Diagonal : ")<<sDiagMin; cout<<("\nGreatest Element of Secondary Diagonal : ")<<sDiagMax; } int main(){ int mat[3][3] = { { 3, 4, 7 }, { 0, 2, 1 }, { 1, 7, 8 } }; int n = sizeof(mat) / sizeof(mat[0]); findMaxAndMinOfDiagonals(mat, n); }
Output
Smallest Element of Principal Diagonal : 2 Greatest Element of Principal Diagonal : 8 Smallest Element of Secondary Diagonal : 1 Greatest Element of Secondary Diagonal : 7
- Related Articles
- C# Program to get the smallest and largest element from a list
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- K’th Smallest/Largest Element using STL in C++
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- K’th Smallest/Largest Element in Unsorted Array in C++
- Kth Smallest Element in a Sorted Matrix in Python
- Program to find area of largest square of 1s in a given matrix in python
- Row-wise common elements in two diagonals of a square matrix in C++
- Find smallest and largest elements in singly linked list in C++
- Program to find smallest intersecting element of each row in a matrix in Python
- Find the Smallest element from a string array in JavaScript
- Find the original matrix when largest element in a row and a column are given in Python
- Program to find nth smallest number from a given matrix in Python
- Program to find Smallest and Largest Word in a String in C++
