Java Program to Interchange Any Two Rows in the Matrix

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

Examples

Input 1: K = 1, L = 2,

          mat[][] = {{2, 1, 4},
                     {1, 2, 3},  
                     {3, 6, 2}}
                     
Output: 
          mat[][] = {{1, 2, 3},
                     {2, 1, 4},  
                     {3, 6, 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 = 2, L = 3,
          mat[][] =  {{2, 1},
                      {1, 2},  
                      {3, 6}}
                     
Output: 
          mat[][] =  {{2, 1},
                     {3, 6},  
                     {1, 2}}

Approach

  • If First and Second are same, then print the matrix as it is.
  • Else Loop over the Kth and Lth row of the matrix.
  • Swap the elements ith index of both the rows while traversal.
  • Now after the loop gets over, print the matrix.

Below is the code implementation for the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to interchange 
// two row 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 exchangeAnyTwoRows(int[][] matrix,
                                          int K, int L)
    {
        for (int i = 0; i < matrix[0].length; i++) {
            
            // Swap two numbers
            int temp = matrix[K - 1][i];
            matrix[K - 1][i] = matrix[L - 1][i];
            matrix[L - 1][i] = temp;
        }
        
        // Print matrix
        printMatrix(matrix);
    }
    
    public static void main(String[] args)
    {
        int K = 2, L = 3;
        int mat[][] = { { 2, 1, 4 }, { 1, 2, 3 }, { 3, 6, 2 } };
  
        // calling the exchange row fuction
        exchangeAnyTwoRows(mat, K, L);
    }
}

chevron_right


Output



2 1 4 
3 6 2 
1 2 3 

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

Space Complexity: 0(1)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.