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++
// 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 Code
int main()
{
int arr[][3]
= { { 3, 2, 1 }, { 4, 5, 6 }, { 9, 8, 7 } };
// Function call
reverseColumnArray(arr);
return 0;
}
Java
// Java implementation of the
// above approach
import java.util.*;
class GFG {
static int M = 3;
static int N = 3;
// A utility function
// for swapping two elements.
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;
}
// Print the 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();
}
}
// Function to reverse
// the given 2D arr[][]
static void reverseColumnArray(int arr[][])
{
// Print the arr[][] before
// reversing every column
printMatrix(arr);
System.out.println();
// 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
arr = swap(arr, start, i, end, i);
// Increment start and decrement
// end for next pair of swapping
start++;
end--;
}
}
// Print the arr[][] after
// reversing every column
printMatrix(arr);
}
// Driver Code
public static void main(String[] args)
{
int arr[][]
= { { 3, 2, 1 }, { 4, 5, 6 }, { 9, 8, 7 } };
// Function call
reverseColumnArray(arr);
}
}
// This code is contributed by 29AjayKumar
Python3
# python implementation of the
# above approach
M = 3
N = 3
# Print the arr[][]
def printMatrix(arr):
for i in range(0, M):
for j in range(0, N):
print(arr[i][j], end=' ')
print()
# Function to reverse
# the given 2D arr[][]
def reverseColumnArray(arr):
# Print the arr[][] before
# reversing every column
printMatrix(arr)
print()
# Traverse each column of arr[][]
for i in range(0, N):
# Initialise start and end index
start = 0
end = M - 1
# Till start < end, swap the
# element at start and end index
while (start < end):
# Swap the element
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 += 1
end -= 1
# Print the arr[][] after
# reversing every column
printMatrix(arr)
# Driver Code
if __name__ == "__main__":
arr = [[3, 2, 1], [4, 5, 6], [9, 8, 7]]
# Function call
reverseColumnArray(arr)
# This code is contributed by rakeshsahni
C#
// C# implementation of the
// above approach
using System;
class GFG {
static int M = 3;
static int N = 3;
// A utility function
// for swapping two elements.
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;
}
// Print the 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("");
}
}
// Function to reverse
// the given 2D arr[][]
static void reverseColumnArray(int[, ] arr)
{
// Print the arr[][] before
// reversing every column
printMatrix(arr);
Console.WriteLine();
// 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
arr = swap(arr, start, i, end, i);
// Increment start and decrement
// end for next pair of swapping
start++;
end--;
}
}
// Print the arr[][] after
// reversing every column
printMatrix(arr);
}
// Driver Code
public static void Main()
{
int[, ] arr
= { { 3, 2, 1 }, { 4, 5, 6 }, { 9, 8, 7 } };
// Function call
reverseColumnArray(arr);
}
}
// This code is contributed by Saurabh Jaiswal
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>
Output3 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
Similar Reads
Program to Interchange or Swap Columns in a Matrix Given a matrix having m rows and n columns, the task is to write a program to interchange any two Columns(i.e. column no. N and M given in the input) in the given matrix. Example: Input : N = 1, M = 2, mat[][] = {{2, 1, 4}, {1, 2, 3}, {3, 6, 2}}Output: mat[][] = {{1, 2, 4}, {2, 1, 3}, {6, 3, 2}} Inp
6 min read
Javascript Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input: 1 2 3
3 min read
Program to reverse an array using pointers Prerequisite : Pointers in C/C++ Given an array, write a program to reverse it using pointers . In this program we make use of * operator . The * (asterisk) operator denotes the value of variable . The * operator at the time of declaration denotes that this is a pointer, otherwise it denotes the val
4 min read
Reverse an array upto a given position Given an array arr[] and a position in array, k. Write a function name reverse (a[], k) such that it reverses subarray arr[0..k-1]. Extra space used should be O(1) and time complexity should be O(k). Example: Input: arr[] = {1, 2, 3, 4, 5, 6} k = 4 Output: arr[] = {4, 3, 2, 1, 5, 6} We strongly reco
6 min read
Convert given upper triangular Matrix to 1D Array Given an upper triangular matrix M[][] of dimensions N * N, the task is to convert it into an one-dimensional array storing only non-zero elements from the matrix. Examples: Input: M[][] = {{1, 2, 3, 4}, {0, 5, 6, 7}, {0, 0, 8, 9}, {0, 0, 0, 10}}Output: Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Colu
12 min read