
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 get maximum and minimum in a Square Matrix in JavaScript, we will be discussing two different approaches, their complexities, and example codes. First approach make use of nested loop while second approach uses Math function.
In this article we are having a 2D square matrix, our task is to write a JavaScript program for maximum and minimum in a square matrix.
Example
Input: Matrix: [ [1, 3, 7], [5, 2, 9], [2, 5, 1] ] Output: Maximum = 9, Minimum = 1
Approaches for Maximum and Minimum in Square Matrix
Here is a list of approaches to write a JavaScript program for maximum and minimum in a square matrix which we will be discussing in this article with stepwise explanation and complete example codes.
Using Brute Force Approach
To get maximum and minimum in a square matrix in javascript, we have used brute force approach we have used nested for loop to iterate over matrix elements and get the minimum and maximum element.
- We have declared a 2D matrix and defined a function min_max() that accepts matrix as argument.
- The length property is used to get the number of rows and columns. We have created two variables, min_ans and max_ans to store the minimum and maximum element.
- The min_ans variable is set to Infinity assuming the elements of the matrix will be less than equal to this. Similalry, max_ans is set to -Infinity.
- Then we have used nested for loop to traverse matrix elements, where outer for loop is used to iterate over rows and inner loop to iterate over columns.
- The if/else condition is used to compare min_ans and max_ans with matrix elements.
- If the value of min_ans is greater than matrix element matrix[i][j], then the matrix element is assigned to min_ans.
- Similalry, if value of matrix element matrix[i][j] is greater than max_ans, then the matrix element is assigned to max_ans.
- After the completion of loop, value of min_ans and max_ans is displayed in web console using console.log() method.
Example
Here is a complete example code implementing above mentioned steps for maximum and minimum in a square matrix using brute force approach.
let matrix = [[1, 3, 7], [5, 2, 9], [2, 5, 1]]; function min_max(matrix) { var rows = matrix.length var cols = matrix[0].length var min_ans = Infinity var max_ans = -Infinity for (var i = 0; i matrix[i][j]) { min_ans = matrix[i][j]; } if (max_ansUsing Math Functions
In this approach to get maximum and minimum in a square matrix in javascript,first we have flattened the 2D matrix into single dimension array and then applied Math functions.
- We have declared a 2D matrix and defined a function min_max() that accepts matrix as argument.
- Then we have used matrix.flat() method which flattens the 2D array into single dimension array. We have stored this single dimension array in array.
- Then we have used Math.max() function to get the maximum and Math.min() to get the minimum value from the array.
- We have used spread operator (...) as we can't directly pass an array to max() or min() function and spread operator expands array elements into individual arguments.
- Then we have returned the max and min value and result is displayed in web console using console.log() method.
Example
Here is a complete example code implementing above mentioned steps for maximum and minimum in a square matrix using Math functions.
let matrix = [[1, 3, 7], [5, 2, 9], [2, 5, 1]]; function min_max(matrix) { const array = matrix.flat(); const max = Math.max(...array); const min = Math.min(...array); return { max, min }; } console.log(min_max(matrix));
Complexity Comparison
Here is a comparison of time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Brute Force | O(n^2) | O(1) |
Math Methods | O(n^2) | O(n^2) |
Conclusion
In this article to write a JavaScript program for maximum and minimum in a square matrix we have used two different approaches. These approaches are: by using brute force approach and by using Math functions.
Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.