Java Program to Interchange Elements of First and Last in a Matrix Across Rows Last Updated : 27 Sep, 2022 Comments Improve Suggest changes 2 Likes Like Report For a given 4 × 4 matrix, the task is to interchange the elements of the first and last rows and then return the resultant matrix. Illustration: Input 1: 1 1 5 0 2 3 7 2 8 9 1 3 6 7 8 2 Output 1: 6 7 8 2 2 3 7 2 8 9 1 3 1 1 5 0 Input 2: 7 8 9 10 11 13 14 1 15 7 12 22 11 21 30 1 Output 2: 11 21 30 1 11 13 14 1 15 7 12 22 7 8 9 10 Approach: To get the required output, we need to swap the elements of the first and the last row of the stated matrix. Example Java // Java Program to Interchange Elements of First // and Last Row in a Matrix // Importing input output classes import java.io.*; // Main Class public class GFG { // Method 1 // To swap First and Last Row static void swap_First_last(int mat[][]) { int rws = mat.length; // Interchanging of elements between the // first and last rows for (int j = 0; j < mat[0].length; j++) { // Using temporary variable so in order // not to lose the values of the matrix // Simply, swapping the values stored int temp = mat[0][j]; mat[0][j] = mat[rws - 1][j]; mat[rws - 1][j] = temp; } } // Method 2 // Main driver method public static void main(String args[]) throws IOException { // Input integer matrix int mat[][] = { { 2, 3, 4, 5 }, { 8, 9, 6, 15 }, { 13, 22, 11, 18 }, { 19, 1, 2, 0 } }; // Display message only System.out.println("Input matrix is as follows : "); // Printing the Input matrix for (int j = 0; j < mat.length; j++) { for (int k = 0; k < mat[0].length; k++) // Print the elements of the input matrix System.out.print(mat[j][k] + " "); // New line as row ended System.out.println(); } System.out.println( "Swapped matrix is as follows : "); // Calling the (method1) to swap rows in a matrix swap_First_last(mat); // Printing the Swapped matrix for (int j = 0; j < mat.length; j++) { for (int k = 0; k < mat[0].length; k++) // Print the elements of the swapped matrix System.out.print(mat[j][k] + " "); // New line as row ended System.out.println(); } } } Output:Input matrix is as follows : 2 3 4 5 8 9 6 15 13 22 11 18 19 1 2 0 Swapped matrix is as follows : 19 1 2 0 8 9 6 15 13 22 11 18 2 3 4 5 Time Complexity: O(n*m) where n and m are numbers of rows and columns respectively.Auxiliary Space: O(1) Create Quiz Comment K Kanchan_Ray Follow 2 Improve K Kanchan_Ray Follow 2 Improve Article Tags : Java Java Programs java-basics Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like