- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Program for Maximum and Minimum in a Square Matrix
To find the maximum or minimum element, we have to focus on the number of comparisons we are going to make and which method will be efficient to choose for comparisons: the one that compares elements with if-else statements or one which comes as in-built. We will see the complete code implementation with the explanation. In this article, we are going to implement a JavaScript program for the maximum and minimum elements present in a given square matrix.
Introduction to Problem
This problem is very simple, but then going into deep will bring some good highlighted concepts that are worth learning.
In the problem, we are given a matrix and for that matrix, we have to find the largest and smallest element present in that. For example, if the matrix is −
Mat = [ 1, 3, 7, 5, 2, 9, 2, 5, 1]
From the above matrix, we can say that 1 is the smallest or the minimum element while 9 is the maximum or largest element.
Let’s see the approach for this problem with code implementation −
Naive Approach
In this approach, we will simply traverse all the elements at once and check if they are greater than our current element or not. Steps will be followed −
First we will create a function to just pass the different matrices and get the result
For the given matrix, we will get its rows and columns to traverse it using the for a loop.
We will create two variables to store the minimum and maximum element and will initialize them with 1000000000 for a minimum element by assuming the elements of the matrix will be less than equal to this.
Also, we will initialize the maximum element with -1000000000 by assuming the elements of the matrix will be greater or equal to this.
Using for loop we will traverse over the matrix and for each index, we will use two if-else conditions.
By making the required comparison we will update the maximum and minimum values.
Example
// creating a function to get the minimum and maximum number function min_max(matrix){ // getting rows and columns of given matrix var rows = matrix.length var cols = matrix[0].length var min_ans = 1000000000 var max_ans = -1000000000 // traversing over the matrix for(var i = 0; i<rows; i++){ for(var j = 0; j<cols; j++){ if(min_ans > matrix[i][j]){ min_ans = matrix[i][j]; } if(max_ans < matrix[i][j]) { max_ans = matrix[i][j]; } } } console.log("The maximum element present in the Matrix is: " + max_ans); console.log("The minimum element present in the Matrix is: " + min_ans); } // defining the matrix Mat = [ [1, 3, 7], [5, 2, 9], [2, 5, 1] ] // calling the function min_max(Mat)
Time and Space Complexity
The time complexity of the above code is O(N*M), where N and M are the rows and columns respectively. Also, the space complexity is O(1).
Here the main issue is not about the time complexity but the number of comparisons we are making. In the above code, we have made the N * M * 2 comparison, because for each index we have checked for minimum and maximum element.
Comparison Efficient Approach
This approach is similar to above approach in most of the parts, but for the comparison part we will now make 3/2 * N* M comparisons by updating some if-else statements. Let’s see the code −
Example
// creating a function to get the minimum and maximum number function min_max(matrix){ // getting rows and columns of given matrix var rows = matrix.length var cols = matrix[0].length var min_ans = 1000000000 var max_ans = -1000000000 // traversing over the matrix for(var i = 0; i<rows; i++){ for(var j = 0; j<=cols/2; j++){ if (matrix[i][j] > matrix[i][rows - j - 1]){ if (min_ans > matrix[i][cols - j - 1]) min_ans = matrix[i][cols - j - 1]; if (max_ans< matrix[i][j]) max_ans = matrix[i][j]; } else { if (min_ans > matrix[i][j]) min_ans = matrix[i][j]; if (max_ans < matrix[i][cols - j - 1]) max_ans = matrix[i][cols - j - 1]; } } } console.log("The maximum element present in the Matrix is: " + max_ans); console.log("The minimum element present in the Matrix is: " + min_ans); } // defining the matrix Mat = [ [1, 3, 7], [5, 2, 9], [2, 5, 1] ] // calling the function min_max(Mat)
Time and Space Complexity
The time complexity of the above code is O(N*M), where N and M are the rows and columns respectively. Also, the space complexity is O(1).
Here the number of comparisons is less as compared to the previous which is 3/2 * (N*M) now.
Conclusion
In this tutorial, we are going to implement a JavaScript program for the maximum and minimum elements present in a given square matrix. We have traversed over the given matrix and compared each element with the variables which store the answer. There were two techniques discussed: one with the number of comparisons 2*N*M while the other with 3/2 *N*M number of comparisons, but both the approaches have the same space complexity.