Wednesday, January 21, 2026
HomeLanguagesJavaJava Program to Interchange Any Two Columns in the Matrix

Java Program to Interchange Any Two Columns in the Matrix

Given a matrix having m rows and n columns. We have to write a Java program to interchange any two Columns(ie column no K and L given in the input) in the given matrix. Swap Operation is required to interchange the elements of two columns. O(1) operation to swap two columns is impossible because complete traversal between two columns is required.

Examples

Input 1: K = 1, L = 2,
        mat[][] = {{2, 1, 4},
                   {1, 2, 3},  
                   {3, 6, 2}}
                 
Output: mat[][] = {{1,2, 4},
                   {2,1, 3},  
                   {6,3, 2}}
                         
Input 2: K = 1, L = 1,
             mat[][] = {{2, 1, 4},
                        {1, 2, 3}}
                         
Output: mat[][] = {{2, 1, 4},
                   {1, 2, 3}}
                          
Input 3: K = 1, L = 3,
              mat[][] = {{2, 1,8},
                         {1, 2,9},  
                         {3, 6,5}}   
                              
Output:    {{8, 1,2},
            {9, 2,1},  
            {5, 6,3}}                        

Approach

  • If K and L ie column number that we have to interchange are the same, then print the matrix as it is.
  • Else Loop over the Kth and Lth column of the matrix.
  • Swap the elements of ith the index of both the columns while doing traversal.
  • Now after the loop gets over, print the matrix.

Below is the code implementation for the above approach:

Java




// Java program to interchange
// two Column in a Matrix
import java.io.*;
class GFG {
 
    public static void printMatrix(int[][] matrix)
    {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++)
                System.out.print(matrix[i][j] + " ");
            System.out.println();
        }
    }
    public static void exchangeAnyTwoColumns(int[][] matrix,
                                             int K, int L)
    {
        for (int i = 0; i < matrix.length; i++) {
 
            // Swap two numbers
            int temp = matrix[i][K - 1];
            matrix[i][K - 1] = matrix[i][L - 1];
            matrix[i][L - 1] = temp;
        }
 
        // Print matrix
        printMatrix(matrix);
    }
 
    public static void main(String[] args)
    {
        int K = 1, L = 2;
        int mat[][]
            = { { 2, 1, 4 }, { 1, 2, 3 }, { 3, 6, 2 } };
 
        // calling the exchange Column function
        exchangeAnyTwoColumns(mat, K, L);
    }
}


Output

1 2 4 
2 1 3 
6 3 2 

Time Complexity: 0(n), Where n is the length of the Column.

Space Complexity: 0(1)

RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS