Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIReverse the rows and columns of a matrix alternatively

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},  
{12, 17, 65, 91},  
{71, 56, 34, 24} 
}
Output: { 
{8,    56,   4, 24},  
{11, 17, 43, 12},  
{91, 65, 23, 21},  
{71,   1, 34,  3} 
}
Explanation: Operations to be followed: 

  • Reverse the first row
  • Reverse the second column
  • Reverse the third row
  • Reverse the fourth row

Input: { {11, 23, 43, 21}, {12, 17, 65, 91}, {71, 56, 34, 24} }
Output: { {21, 56, 23, 71}, {12, 17, 65, 91}, {24, 34, 43, 11} } 

 

Approach: The task can be solved by simply running two while loops for traversing rows and columns alternatively. In the end, print the resultant matrix.

Below is the implementation of the above approach:

C++




// C++ program to find Reverse the
// rows and columns of a matrix alternatively.
#include <bits/stdc++.h>
using namespace std;
const int N = 4;
const int M = 4;
 
// Print matrix elements
void showArray(int arr[][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 rows and columns
// of a matrix alternatively.
void reverseAlternate(int arr[][N])
{
    int turn = 0;
 
    while (turn < M && turn < N) {
        if (turn % 2 == 0) {
            int start = 0, end = N - 1, temp;
            while (start < end) {
                temp = arr[turn][start];
                arr[turn][start] = arr[turn][end];
                arr[turn][end] = temp;
                start += 1;
                end -= 1;
            }
            turn += 1;
        }
 
        if (turn % 2 == 1) {
            int start = 0, end = M - 1, temp;
            while (start < end) {
                temp = arr[start][turn];
                arr[start][turn] = arr[end][turn];
                arr[end][turn] = temp;
                start += 1;
                end -= 1;
            }
            turn += 1;
        }
    }
}
 
// Driver code
int main()
{
 
    int matrix[][N] = { { 3, 4, 1, 8 },
                        { 11, 23, 43, 21 },
                        { 12, 17, 65, 91 },
                        { 71, 56, 34, 24 } };
    reverseAlternate(matrix);
    showArray(matrix);
}


Java




// Java program for the above approach
import java.util.*;
public class GFG
{
  static int N = 4;
  static int M = 4;
 
  // Print matrix elements
  static void showArray(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 rows and columns
  // of a matrix alternatively.
  static void reverseAlternate(int arr[][])
  {
    int turn = 0;
 
    while (turn < M && turn < N) {
      if (turn % 2 == 0) {
        int start = 0, end = N - 1, temp;
        while (start < end) {
          temp = arr[turn][start];
          arr[turn][start] = arr[turn][end];
          arr[turn][end] = temp;
          start += 1;
          end -= 1;
        }
        turn += 1;
      }
 
      if (turn % 2 == 1) {
        int start = 0, end = M - 1, temp;
        while (start < end) {
          temp = arr[start][turn];
          arr[start][turn] = arr[end][turn];
          arr[end][turn] = temp;
          start += 1;
          end -= 1;
        }
        turn += 1;
      }
    }
  }
 
  // Driver Code
  public static void main(String args[])
  {
 
    int matrix[][] = { { 3, 4, 1, 8 },
                      { 11, 23, 43, 21 },
                      { 12, 17, 65, 91 },
                      { 71, 56, 34, 24 } };
    reverseAlternate(matrix);
    showArray(matrix);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3




# Python3 program to find Reverse the
# rows and columns of a matrix alternatively.
N = 4
M = 4
 
# Print matrix elements
def showArray(arr):
 
    for i in range(M):
        for j in range(N):
            print(arr[i][j], end = " ")
             
        print()
 
# Function to Reverse the rows and columns
# of a matrix alternatively.
def reverseAlternate(arr):
     
    turn = 0
 
    while turn < M and turn < N:
        if (turn % 2 == 0):
            start = 0
            end = N - 1
 
            while (start < end):
                temp = arr[turn][start]
                arr[turn][start] = arr[turn][end]
                arr[turn][end] = temp
                start += 1
                end -= 1
 
            turn += 1
 
        if (turn % 2 == 1):
            start = 0
            end = M - 1
 
            while (start < end):
                temp = arr[start][turn]
                arr[start][turn] = arr[end][turn]
                arr[end][turn] = temp
                start += 1
                end -= 1
 
            turn += 1
 
# Driver code
matrix = [ [ 3, 4, 1, 8 ],
           [ 11, 23, 43, 21 ],
           [ 12, 17, 65, 91 ],
           [ 71, 56, 34, 24 ] ]
            
reverseAlternate(matrix)
showArray(matrix)
 
# This code is contributed by Potta Lokesh


C#




// C# program to find Reverse the
// rows and columns of a matrix alternatively.
using System;
class GFG {
  const int N = 4;
  const int M = 4;
 
  // Print matrix elements
  static void showArray(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 rows and columns
  // of a matrix alternatively.
  static void reverseAlternate(int[, ] arr)
  {
    int turn = 0;
 
    while (turn < M && turn < N) {
      if (turn % 2 == 0) {
        int start = 0, end = N - 1, temp;
        while (start < end) {
          temp = arr[turn, start];
          arr[turn, start] = arr[turn, end];
          arr[turn, end] = temp;
          start += 1;
          end -= 1;
        }
        turn += 1;
      }
 
      if (turn % 2 == 1) {
        int start = 0, end = M - 1, temp;
        while (start < end) {
          temp = arr[start, turn];
          arr[start, turn] = arr[end, turn];
          arr[end, turn] = temp;
          start += 1;
          end -= 1;
        }
        turn += 1;
      }
    }
  }
 
  // Driver code
  public static void Main()
  {
 
    int[, ] matrix = { { 3, 4, 1, 8 },
                      { 11, 23, 43, 21 },
                      { 12, 17, 65, 91 },
                      { 71, 56, 34, 24 } };
    reverseAlternate(matrix);
    showArray(matrix);
  }
}
 
// This code is contributed by ukasp.


Javascript




<script>
    // JavaScript program to find Reverse the
    // rows and columns of a matrix alternatively.
    const N = 4;
    const M = 4;
 
    // Print matrix elements
    const showArray = (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 rows and columns
    // of a matrix alternatively.
    const reverseAlternate = (arr) => {
        let turn = 0;
 
        while (turn < M && turn < N) {
            if (turn % 2 == 0) {
                let start = 0, end = N - 1, temp;
                while (start < end) {
                    temp = arr[turn][start];
                    arr[turn][start] = arr[turn][end];
                    arr[turn][end] = temp;
                    start += 1;
                    end -= 1;
                }
                turn += 1;
            }
 
            if (turn % 2 == 1) {
                let start = 0, end = M - 1, temp;
                while (start < end) {
                    temp = arr[start][turn];
                    arr[start][turn] = arr[end][turn];
                    arr[end][turn] = temp;
                    start += 1;
                    end -= 1;
                }
                turn += 1;
            }
        }
    }
 
    // Driver code
    let matrix = [[3, 4, 1, 8],
    [11, 23, 43, 21],
    [12, 17, 65, 91],
    [71, 56, 34, 24]];
    reverseAlternate(matrix);
    showArray(matrix);
 
    // This code is contributed by rakeshsahni
 
</script>


Output

8 56 4 24 
11 17 43 12 
91 65 23 21 
71 1 34 3 

Time Complexity: O(M*N)
Space Complexity: O(1), no additional extra space is used.

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments