Program to reverse columns in given 2D Array (Matrix)
Given a 2D array arr[][]of integers of size M x N, where N is the number of columns and M is the number of rows in the array. The task is to reverse every column of the given 2D array.
Input: arr[][] =
Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out our self-paced courses designed for students of grades I-XII.
Start with topics like Python, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, because GeeksforGeeks School is now here!
{{3, 2, 1}
{4, 5, 6},
{9, 8, 7}}
Output:
9 8 7
4 5 6
3 2 1
Input: arr[][] =
{{7, 9},
{1, 5},
{4, 6},
{19, 3}}
Output:
19 3
4 6
1 5
7 9
Approach: For each and every column in the given 2 Dimensional Array or Matrix, do the following steps:
- Initialize the start index as 0 and end index as M – 1.
- Iterate a for loop till the start index is less than the ending index, swap the value at these indexes and update the index as:
swap(arr[start][i], arr[end][i]); start++; end--;
Below is the implementation of the above algorithm:
C++
// C++ implementation of the// above approach#include <bits/stdc++.h>using namespace std;const int M = 3;const int N = 3;// A utility function// for swapping two elements.void swap(int* a, int* b){ int temp = *a; *a = *b; *b = temp;}// Print the arr[][]void printMatrix(int arr[M][N]){ for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { cout << arr[i][j] << ' '; } cout << endl; }}// Function to reverse// the given 2D arr[][]void reverseColumnArray(int arr[M][N]){ // Print the arr[][] before // reversing every column printMatrix(arr); cout << endl; // Traverse each column of arr[][] for (int i = 0; i < N; i++) { // Initialise start and end index int start = 0; int end = M - 1; // Till start < end, swap the // element at start and end index while (start < end) { // Swap the element swap(&arr;[start][i], &arr;[end][i]); // Increment start and decrement // end for next pair of swapping start++; end--; } } // Print the arr[][] after // reversing every column printMatrix(arr);}// Driver Codeint main(){ int arr[][3] = { { 3, 2, 1 }, { 4, 5, 6 }, { 9, 8, 7 } }; // Function call reverseColumnArray(arr); return 0;} |
Javascript
<script> // JavaScript code for the above approach let M = 3; let N = 3; // Print the arr[][] function printMatrix(arr) { for (let i = 0; i < M; i++) { for (let j = 0; j < N; j++) { document.write(arr[i][j] + ' '); } document.write('<br>') } } // Function to reverse // the given 2D arr[][] function reverseColumnArray(arr) { // Print the arr[][] before // reversing every column printMatrix(arr); document.write('<br>') // Traverse each column of arr[][] for (let i = 0; i < N; i++) { // Initialise start and end index let start = 0; let end = M - 1; // Till start < end, swap the // element at start and end index while (start < end) { // Swap the element let temp = arr[start][i] arr[start][i] = arr[end][i] arr[end][i] = temp // Increment start and decrement // end for next pair of swapping start++; end--; } } // Print the arr[][] after // reversing every column printMatrix(arr); } // Driver Code let arr = [[3, 2, 1], [4, 5, 6], [9, 8, 7]]; // Function call reverseColumnArray(arr); // This code is contributed by Potta Lokesh </script> |
3 2 1 4 5 6 9 8 7 9 8 7 4 5 6 3 2 1
Time complexity: O(N2), two for loops are being used, one to iterate over all columns and the inner loop is swapping the start and end index of each column.
Auxiliary Complexity: 0(1) as no extra space, not even for swapping the two elements.

