Reversing the columns of a Matrix

PHOTO EMBED

Tue Feb 08 2022 08:47:50 GMT+0000 (Coordinated Universal Time)

Saved by @Uttam #java #gfg #geeksforgeeks #2d #array #matrix #practice #reversingcolumns

class Solution
{
    //Function to reverse the columns of a matrix.
    static void reverseCol(int matrix[][])
    {
       for(int i=0; i<matrix.length; i++){
           for(int j=0; j<matrix[i].length/2; j++)
           {
               int temp = matrix[i][j];
               matrix[i][j] = matrix[i][matrix[i].length-j-1];
               matrix[i][matrix[i].length-j-1] = temp;
           }
       } 
    }
}
content_copyCOPY

Reversing the columns of a Matrix Given a matrix of dimensions n1 x m1. Reverse its columns in-place such that the last column will become the first column and so on. Example 1: Input: n1 = 4, m1 = 3 matrix[][] = {{ 1, 2, 3}, { 5, 6, 7}, {11, 10, 9}, {13, 14, 15}} Output: 3 2 1 7 6 5 9 10 11 15 14 13 Explanation: Array after exchanging columns: 3 2 1 7 6 5 9 10 11 15 14 13 Example 2: Input: n1 = 2, m1 = 5 matrix[][] = {{ 1, 2, 3, 4, 5}, { 6, 7, 8, 9, 10}} Output: 5 4 3 2 1 10 9 8 7 6 Explanation: After reversing the column of matrix 5 4 3 2 1 10 9 8 7 6 Your Task: You dont need to read input or print anything. Complete the function reverseCol() that takes matrix as input parameter and modifies it in-place such that the last column will become the first column and so on. Expected Time Complexity: O(n1 * m1) Expected Auxiliary Space: O(1) Constraints: 1 <= n1, m1 <= 100 0 <= matrix[i][j] <= 1000

https://practice.geeksforgeeks.org/problems/reversing-the-columns-of-a-matrix-1587115621/1/?track=DSASP-Matrix&batchId=190