Javascript Program to Interchange elements of first and last rows in matrix Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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! Create Quiz Comment K kartik Follow 0 Improve K kartik Follow 0 Improve Article Tags : JavaScript JavaScript-Program Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like