Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. The first approach uses nested loops while the second approach uses Math functions.
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.
Problem Statement
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 are the two approaches to write a JavaScript program for maximum and minimum in a square matrix:
Using Nested Loops (Brute Force)
In this approach, we use nested for loops to iterate through each element of the matrix and compare it with current minimum and maximum values.
Algorithm Steps
- Initialize min_ans to
Infinityand max_ans to-Infinity - Use nested loops to traverse all matrix elements
- Compare each element with current min and max values
- Update min_ans and max_ans accordingly
Example
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 < 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("Maximum element: " + max_ans);
console.log("Minimum element: " + min_ans);
}
min_max(matrix);
Maximum element: 9 Minimum element: 1
Using Math Functions
This approach flattens the 2D matrix into a single-dimensional array and then applies Math functions to find minimum and maximum values.
Algorithm Steps
- Use
matrix.flat()to convert 2D array to 1D array - Apply Math.max() and Math.min() with spread operator
- Return the results
Example
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 };
}
const result = min_max(matrix);
console.log("Maximum element: " + result.max);
console.log("Minimum element: " + result.min);
Maximum element: 9 Minimum element: 1
Complexity Comparison
Here is a comparison of time and space complexity of both approaches:
| Approach | Time Complexity | Space Complexity | Memory Usage |
|---|---|---|---|
| Nested Loops | O(n²) | O(1) | Constant space |
| Math Functions | O(n²) | O(n²) | Extra array created |
Key Points
- Nested loops approach: More memory efficient but slightly more code
- Math functions approach: Cleaner code but uses additional memory
- Both approaches have the same time complexity O(n²) for an n×n matrix
- Choose nested loops for large matrices to save memory
Conclusion
Both approaches effectively find maximum and minimum values in a square matrix. The nested loops approach is more memory efficient, while the Math functions approach offers cleaner, more readable code. Choose based on your memory constraints and code readability preferences.
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.
