Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaJava Program to Interchange Elements of First and Last in a Matrix...

Java Program to Interchange Elements of First and Last in a Matrix Across Columns

For a given 4 x 4 matrix, the task is to interchange the elements of the first and last columns and then return the resultant matrix.

Examples :

Input 1 :   1  1  5  0
            2  3  7  2
            8  9  1  3
            6  7  8  2

Output 1 :  0  1  5  1
            2  3  7  2
            3  9  1  8
            2  7  8  6
 
Input 2 :   7   8  9  10
           11  13  14  1
           15   7  12 22
           11  21  30  1

Output 2 : 10  8   9   7
           1  13  14  11
           22  7  12  15
           1  21  30  11

Approach:

To get the required output, we need to swap the elements of the first and last column of the stated matrix.

Example

Java




// Java Program to Interchange Elements of the
// First and Last Column in a Matrix
 
// Importing input output classes
import java.io.*;
 
class GFG {
 
    // Declare static variable and initialize to
    // order of the matrix
    static int N = 3;
 
    // Method 1
    // To swap first and last column in a matrix
    static void Swap_First_Last(int mat[][])
    {
        int cls = N;
 
        // Interchanging of elements between the
        // first and last columns
        for (int j = 0; j < N; j++) {
            int temp = mat[j][0];
            mat[j][0] = mat[j][N - 1];
            mat[j][N - 1] = temp;
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Creating 2D integer element matrix
        // Custom input matrix
        int mat[][]
            = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
 
        // Outer loop for rows
        for (int j = 0; j < N; j++) {
 
            // Inner loop for columns
            for (int k = 0; k < N; k++) {
 
                // Print the input matrix
                System.out.print(mat[j][k] + " ");
            }
            // Operations over a row is computed so new line
            System.out.println();
        }
 
        System.out.println("Swapped Matrix as follows : ");
 
        // Now, calling the (Method1) to interchange
        // first and last columns in above matrix
        Swap_First_Last(mat);
 
        // Now simply print the updated matrix
        // Swapped matrix using nested for loops
 
        // Outer loop for rows
        for (int j = 0; j < N; j++) {
 
            // Inner loop for columns
            for (int k = 0; k < N; k++)
 
                // Print the swapped matrix
                System.out.print(mat[j][k] + " ");
 
            // Operations over a row is computed so new line
            System.out.println();
        }
    }
}


Output

1 2 3 
4 5 6 
7 8 9 
Swapped Matrix as follows : 
3 2 1 
6 5 4 
9 8 7 

Time Complexity: O(N2)

Auxiliary Space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments