Javascript Program for Maximum and Minimum in a square matrix. Last Updated : 20 Mar, 2023 Summarize Comments Improve Suggest changes Share 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. JavaScript // JavaScript Program for finding maximum // and minimum in a matrix function maxMin(arr, n){ let min = +2147483647; let max = -2147483648; // loop to find the maximum element for(let i = 0; i<n; i++){ for(let j = 0; j<n; j++){ if(max < arr[i][j]) max = arr[i][j]; } } // for finding the minimum element for(let i = 0; i<n; i++){ for(let j = 0; j<n; j++){ if(min > arr[i][j]) min = arr[i][j]; } } console.log("Maximum = " + max + ", Minimum = " + min); } // driver code for above approach let arr = [ [ 5, 9, 11 ], [ 25, 0, 14 ], [ 21, 6, 4 ] ]; maxMin(arr, 3); // THIS CODE IS CONTRIBUTED BY KIRTI AGARWAL(KIRTIAGARWAL23121999) OutputMaximum = 25, Minimum = 0 Time Complexity: O(N^2) Auxiliary Space: O(1) 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. JavaScript <script> // Javascript program for finding maximum // and minimum in a matrix. let MAX = 100; // Finds maximum and minimum // in arr[0..n-1][0..n-1] // using pair wise comparisons function maxMin(arr,n) { let min = +2147483647; let max = -2147483648; // Traverses rows one by one for(let i = 0; i < n; i++) { for(let 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]; } } } document.write("Maximum = " + max + ", Minimum = " + min); } // Driver Code let arr = [ [ 5, 9, 11 ], [ 25, 0, 14 ], [ 21, 6, 4 ] ]; maxMin(arr, 3); // This code is contributed by sravan kumar </script> Output: Maximum = 25, Minimum = 0 Time Complexity: O(N^2) Auxiliary Space: O(1) Please refer complete article on Maximum and Minimum in a square matrix. for more details! Comment More infoAdvertise with us K kartik Follow Improve Article Tags : JavaScript Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at 7 min read JSON Web Token (JWT) A JSON Web Token (JWT) is a standard used to securely transmit information between a client (like a frontend application) and a server (the backend). It is commonly used to verify users' identities, authenticate them, and ensure safe communication between the two. JWTs are mainly used in web apps an 7 min read Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie 15+ min read JavaScript Coding Questions and Answers JavaScript is the most commonly used interpreted, and scripted Programming language. It is used to make web pages, mobile applications, web servers, and other platforms. Developed in 1995 by Brendan Eich. Developers should have a solid command over this because many job roles need proficiency in Jav 15+ min read Top 95+ Javascript Projects For 2025 JavaScript is a lightweight, cross-platform programming language that powers dynamic and interactive web content. From real-time updates to interactive maps and animations, JavaScript brings web pages to life.Here, we provided 95+ JavaScript projects with source code and ideas to provide hands-on ex 4 min read Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9)); // output: 15Function Syntax 5 min read JavaScript Exercises, Practice Questions and Solutions JavaScript Exercise covers interactive quizzes, tracks progress, and enhances coding skills with our engaging portal. Ideal for beginners and experienced developers, Level up your JavaScript proficiency at your own pace. Start coding now! A step-by-step JavaScript practice guide for beginner to adva 3 min read HTML DOM (Document Object Model) The HTML DOM (Document Object Model) is a programming interface that represents the structure of a web page in a way that programming languages like JavaScript can understand and manipulate. Think of it as a tree of objects where each part of your HTML document (elements, attributes, text) is repres 6 min read Like