Program to reverse columns in given 2D Array (Matrix)
Last Updated :
19 Sep, 2022
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[][] = {{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
Program to reverse columns in given 2D Array (Matrix) using swap function:
Follow the given steps to solve the problem:
- Initialize the start index as 0 and the 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 approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int M = 3;
const int N = 3;
void swap( int * a, int * b)
{
int temp = *a;
*a = *b;
*b = temp;
}
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;
}
}
void reverseColumnArray( int arr[M][N])
{
printMatrix(arr);
cout << endl;
for ( int i = 0; i < N; i++) {
int start = 0;
int end = M - 1;
while (start < end) {
swap(&arr[start][i], &arr[end][i]);
start++;
end--;
}
}
printMatrix(arr);
}
int main()
{
int arr[][3]
= { { 3, 2, 1 }, { 4, 5, 6 }, { 9, 8, 7 } };
reverseColumnArray(arr);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int M = 3 ;
static int N = 3 ;
private static int [][] swap( int [][] arr, int start,
int i, int end, int j)
{
int temp = arr[start][i];
arr[start][i] = arr[end][j];
arr[end][j] = temp;
return arr;
}
static void printMatrix( int arr[][])
{
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++) {
System.out.print(arr[i][j] + " " );
}
System.out.println();
}
}
static void reverseColumnArray( int arr[][])
{
printMatrix(arr);
System.out.println();
for ( int i = 0 ; i < N; i++) {
int start = 0 ;
int end = M - 1 ;
while (start < end) {
arr = swap(arr, start, i, end, i);
start++;
end--;
}
}
printMatrix(arr);
}
public static void main(String[] args)
{
int arr[][]
= { { 3 , 2 , 1 }, { 4 , 5 , 6 }, { 9 , 8 , 7 } };
reverseColumnArray(arr);
}
}
|
Python3
M = 3
N = 3
def printMatrix(arr):
for i in range ( 0 , M):
for j in range ( 0 , N):
print (arr[i][j], end = ' ' )
print ()
def reverseColumnArray(arr):
printMatrix(arr)
print ()
for i in range ( 0 , N):
start = 0
end = M - 1
while (start < end):
temp = arr[start][i]
arr[start][i] = arr[end][i]
arr[end][i] = temp
start + = 1
end - = 1
printMatrix(arr)
if __name__ = = "__main__" :
arr = [[ 3 , 2 , 1 ], [ 4 , 5 , 6 ], [ 9 , 8 , 7 ]]
reverseColumnArray(arr)
|
C#
using System;
class GFG {
static int M = 3;
static int N = 3;
private static int [, ] swap( int [, ] arr, int start,
int i, int end, int j)
{
int temp = arr[start, i];
arr[start, i] = arr[end, j];
arr[end, j] = temp;
return arr;
}
static void printMatrix( int [, ] arr)
{
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
Console.Write(arr[i, j] + " " );
}
Console.WriteLine( "" );
}
}
static void reverseColumnArray( int [, ] arr)
{
printMatrix(arr);
Console.WriteLine();
for ( int i = 0; i < N; i++) {
int start = 0;
int end = M - 1;
while (start < end) {
arr = swap(arr, start, i, end, i);
start++;
end--;
}
}
printMatrix(arr);
}
public static void Main()
{
int [, ] arr
= { { 3, 2, 1 }, { 4, 5, 6 }, { 9, 8, 7 } };
reverseColumnArray(arr);
}
}
|
Javascript
<script>
let M = 3;
let N = 3;
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 reverseColumnArray(arr) {
printMatrix(arr);
document.write( '<br>' )
for (let i = 0; i < N; i++)
{
let start = 0;
let end = M - 1;
while (start < end)
{
let temp = arr[start][i]
arr[start][i] = arr[end][i]
arr[end][i] = temp
start++;
end--;
}
}
printMatrix(arr);
}
let arr
= [[3, 2, 1], [4, 5, 6], [9, 8, 7]];
reverseColumnArray(arr);
</script>
|
Output
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 Space: O(1) as no extra space is used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...