Matrix Interchange - Java | Practice | GeeksforGeeks

Working with 2D arrays is quite important. Here we will do swapping of column in a 2D array. You are given a matrix M or r rows and c columns. You need to swap the first column with the last column.
Example:
Input:
3 4
1 2 3 4
4 3 2 1
6 7 8 9

Output:
4 2 3 1
1 3 2 4
9 7 8 6
Your Task:
Since this is a function problem, you don't need to take any input. Just complete the provided function interchange(int, int , int ) that take matrix, rows and columns as parameters.
Constraints:
1 <= r,c <= 100
0
newlife290420043 weeks ago
static void interchange(int a[][],int r, int c){              // Your code here       int [][]new_a = new int[r][c];       int temp = 0;       for(int i = 0;i<r;i++){           for(int j=0;j<c;j++){               if(j<1){               int v = c-1;               temp = a[i][j];               a[i][j] = a[i][v];               a[i][v] = temp;               v--;               }                          }       }        // print the array        for(int i = 0;i<r;i++){           for(int j=0;j<c;j++){               System.out.print(a[i][j]+" ");           }           System.out.println();       }    }}
0
rahul8781 month ago
 static void interchange(int a[][],int r, int c){
        
         for(int i = 0;i<r;i++){
            for(int j = 0;j<c;j++){
               if(j==0){
                   int temp=a[i][j];
                   a[i][j]=a[i][c-1];
                   a[i][c-1]=temp;
               }
            }
            //System.out.println();
        } 
        
        for(int i = 0;i<r;i++){
            for(int j = 0;j<c;j++){
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        } 
    }
0
patelshivanshu20172 months ago
 static void interchange(int a[][],int r, int c){              // Your code here        int i=0;       while(i<r){          int temp=a[i][0];          a[i][0]=a[i][c-1];          a[i][c-1]=temp;          i++;       }       for( i = 0;i<r;i++){           for(int j = 0;j<c;j++){               System.out.print(a[i][j] + " ");           }           System.out.println();       }    }}
0
swapniltayal4222 months ago
class Geeks{      static void interchange(int a[][],int r, int c){              // Your code here       for (int i = 0; i < r; i++ ){           int temp = a[i][0] ;           a[i][0] = a[i][c-1] ;           a[i][c-1] = temp ;                  }       for (int i = 0; i < r; i++ ){           for (int j = 0; j < c; j++ ){               System.out.print(a[i][j] + " ");           }System.out.println();       }   }}
0
sowndhar5252 months ago
class Geeks{
    
    static void interchange(int a[][],int r, int c){
        
        for(int i = 0; i < r; i++){
            int temp = a[i][0];
            a[i][0] = a[i][c-1];
            a[i][c-1] = temp;
        }
        
        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                System.out.print(a[i][j]+" ");
            }
        System.out.println();
        }
    }
}
0
abhiofficial4 months ago
static void interchange(int a[][],int r, int c){              // Your code here              for(int i = 0;i<r;i++){           for(int j = 0;j<c;j++){               if(j==0){                   System.out.print(a[i][c-1] + " ");               }               else if(j==c-1){                   System.out.print(a[i][0] + " ");               }               else               System.out.print(a[i][j] + " ");           }           System.out.println();       }    }
0
rajadash23454 months ago
static void interchange(int a[][],int r, int c){              // Your code here             int temp=0;       for(int i = 0;i<r;i++){                       temp =a[i][0];              a[i][0]=a[i][c-1];              a[i][c-1]=temp;              for(int j = 0;j<c;j++){              System.out.print(a[i][j] + " ");           }           System.out.println();       }    }
}
0
mrpascal4 months ago
Simple Solution
static void interchange(int a[][],int r, int c){
        
        // Your code here
        
            for(int j = 0;j<r;j++){
                int temp = a[j][0];
                a[j][0] = a[j][c-1];
                a[j][c-1] = temp;
            }
        
        
        for(int i = 0;i<r;i++){
            for(int j = 0;j<c;j++){
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        } 
    }
0
junadmd545 months ago
// JAVA SOLUTION
class Geeks{      static void interchange(int a[][],int r, int c){       int j1,j2;       for(int i =0;i<r;i++){           if(r==1&&c==1)           break;           j1=0;           j2=c-1;              a[i][j1]=a[i][j1]+a[i][j2];              a[i][j2]=a[i][j1]-a[i][j2];              a[i][j1]=a[i][j1]-a[i][j2];       }       // Your code here              for(int i = 0;i<r;i++){           for(int j = 0;j<c;j++){               System.out.print(a[i][j] + " ");           }           System.out.println();       }    }}
0
mashhadihossain6 months ago
SIMPLE JAVA SOLUTION
static void interchange(int a[][],int r, int c){              for(int i=0;i<r;i++)       {           int temp=a[i][0];           a[i][0]=a[i][c-1];           a[i][c-1]=temp;       }                                   for(int i = 0;i<r;i++){           for(int j = 0;j<c;j++){               System.out.print(a[i][j] + " ");           }           System.out.println();       }    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.