Javascript Program to Interchange elements of first and last rows in matrix Last Updated : 12 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a 4 x 4 matrix, we have to interchange the elements of first and last row and show the resulting matrix.Examples : Input : 3 4 5 0 2 6 1 2 2 7 1 2 2 1 1 2Output : 2 1 1 2 2 6 1 2 2 7 1 2 3 4 5 0Input : 9 7 5 1 2 3 4 1 5 6 6 5 1 2 3 1Output : 1 2 3 1 2 3 4 1 5 6 6 5 9 7 5 1The approach is very simple, we can simply swap the elements of first and last row of the matrix inorder to get the desired matrix as output.Below is the implementation of the approach : JavaScript // Javascript code to swap the element of first // and last row and display the result function interchangeFirstLast(m) { let rows = m.length; // swapping of element between first // and last rows for (let i = 0; i < m[0].length; i++) { let t = m[0][i]; m[0][i] = m[rows - 1][i]; m[rows - 1][i] = t; } } // Driver code // input in the array let m = [[8, 9, 7, 6], [4, 7, 6, 5], [3, 2, 1, 8], [9, 9, 7, 7]] interchangeFirstLast(m); // printing the interchanged matrix for (let i = 0; i < m.length; i++) { let output=""; for (let j = 0; j < m[0].length; j++) output += m[i][j] + " "; console.log(output); } // This code is contributed by avanitrachhadiya2155 Output9 9 7 7 4 7 6 5 3 2 1 8 8 9 7 6 Complexity Analysis:Time Complexity: O(N), where N is no of rows; as we are using single loop for interchanging first and last rows of given matrix.Auxiliary Space: O(1), as we are not using any extra space.Please refer complete article on Interchange elements of first and last rows in matrix for more details! Comment More infoAdvertise with us Next Article Javascript Program to Interchange elements of first and last rows in matrix K kartik Follow Improve Article Tags : JavaScript JavaScript-Program 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