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
Reverse the rows and columns of a matrix alternatively Given a matrix arr[][] of size M*N, where M is the number of rows and N is the number of columns. The task is to reverse the rows and columns of the matrix alternatively i.e start with reversing the 1st row, then the 2nd column, and so on. Examples: Input: arr[][] = { {3, 4, 1, 8}, {11, 23, 43, 21},
7 min read
Print a matrix in Reverse Wave Form Given a matrix, print it in Reverse Wave Form. Examples : Input : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output : 4 8 12 16 15 11 7 3 2 6 10 14 13 9 5 1 Input : 1 9 4 10 3 6 90 11 2 30 85 72 6 31 99 15 Output : 10 11 72 15 99 85 90 4 9 6 30 31 6 2 3 1 Approach :To get the reverse wave form for a giv
8 min read
Convert given lower triangular Matrix to 1D array Given a lower triangular matrix M[][] of dimension N * N, the task is to convert it into a one-dimensional array by storing only non-zero elements. Examples: Input: M[][] = {{1, 0, 0, 0}, {2, 3, 0, 0}, {4, 5, 6, 0}, {7, 8, 9, 10}} Output:Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}Column-wise: {1, 2, 4
13 min read
Sort the Matrix based on the given column number Given a Matrix of size M * N (0-indexed) and an integer K, the matrix contains distinct integers only, the task is to sort the Matrix (i.e., the rows of the matrix) by their Values in the Kth column, from Maximum to Minimum. Return the matrix after sorting it. Examples: Input: Matrix = [[10, 6, 9, 1
12 min read
Search in matrix where only individual columns are sorted Given an n * m semi-sorted 2D matrix, mat[][] where each column is individually sorted in ascending order but the rows are not sorted, and a target element x. The task is to find the position (row and column index) of x in the matrix. If the element is not found, return -1.Examples: Input: mat[][] =
7 min read