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 1Input: 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++ 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 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 |
# 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# 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 |
<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 Space: O(1) as no extra space is used