Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmFlip the given Matrix along both diagonals in clockwise direction

Flip the given Matrix along both diagonals in clockwise direction

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 flip the matrix by both diagonals. Flipping the matrix means rotating all elements of the matrix in a clockwise direction, along the diagonal

Examples:

Input: arr[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }
Output: { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} }
Explanation: Resultant matrix after flipping the matrix along the main diagonal: { {1, 4, 7}, {2, 5, 8}, {3, 6, 9} }
Resultant matrix after flipping the matrix along the second diagonal: { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} }

Input: arr[][] = { {1,  2,  3,  4}, {5,  6,  7,  8}, {9,  10, 11, 12}, {13, 14, 15, 16} }
Output: { {16, 15, 14, 13}, {12, 11, 10, 9}, {8, 7, 6, 5}, {4, 3, 2, 1} }

 

Approach: The task can easily be solved using observations. One can observe that the resultant matrix would contain reversed rows in reverse order. Follow the below steps to solve the problem:

  • Iterate over the rows of the matrix, and swap elements of the first row & last row in reverse order, and similarly second row & second last row, and so on.

Below is the implementation of the above code:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
const int m = 4, n = 4;
 
// Function to flip the matrix along
// both the diagonals
void flip(int matrix[m][n])
{
    int i, j, temp;
       
      // Swapping elements
    for (i = 0; i < m / 2; i++) {
        for (j = 0; j < n; j++) {
            temp = matrix[i][j];
            matrix[i][j] = matrix[m - 1 - i][n - 1 - j];
            matrix[m - 1 - i][n - 1 - j] = temp;
        }
    }
}
 
// Function to print the matrix
void show(int matrix[m][n])
{
    int i, j;
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            cout << matrix[i][j] << "  ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
 
    int matrix[4][4] = { { 1, 2, 3, 4 },
                         { 5, 6, 7, 8 },
                         { 9, 10, 11, 12 },
                         { 13, 14, 15, 16 } };
 
    flip(matrix);
    show(matrix);
}


Java




// Java program for the above approach
import java.util.*;
class GFG{
 
static int m = 4, n = 4;
 
// Function to flip the matrix along
// both the diagonals
static void flip(int matrix[][])
{
    int i, j, temp;
       
      // Swapping elements
    for (i = 0; i < m / 2; i++) {
        for (j = 0; j < n; j++) {
            temp = matrix[i][j];
            matrix[i][j] = matrix[m - 1 - i][n - 1 - j];
            matrix[m - 1 - i][n - 1 - j] = temp;
        }
    }
}
 
// Function to print the matrix
static void show(int matrix[][])
{
    int i, j;
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            System.out.print(matrix[i][j]+ "  ");
        }
        System.out.println();
    }
}
 
// Driver Code
public static void main(String[] args)
{
 
    int matrix[][] = { { 1, 2, 3, 4 },
                         { 5, 6, 7, 8 },
                         { 9, 10, 11, 12 },
                         { 13, 14, 15, 16 } };
    flip(matrix);
    show(matrix);
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python3 program for the above approach
m = 4
n = 4
 
# Function to flip the matrix along
# both the diagonals
def flip(matrix):
     
    i = None
    j = None
    temp = None
 
    # Swapping elements
    for i in range(m // 2):
        for j in range(n):
            temp = matrix[i][j]
            matrix[i][j] = matrix[m - 1 - i][n - 1 - j]
            matrix[m - 1 - i][n - 1 - j] = temp
 
# Function to print the matrix
def show(matrix):
     
    i = None
    j = None
    for i in range(m):
        for j in range(n):
            print(matrix[i][j], end = "  ")
             
        print("")
 
# Driver Code
matrix = [ [ 1, 2, 3, 4 ],
           [ 5, 6, 7, 8 ],
           [ 9, 10, 11, 12 ],
           [ 13, 14, 15, 16 ] ]
 
flip(matrix)
show(matrix)
 
# This code is contributed by gfgking


C#




// C# program for the above approach
using System;
class GFG
{
  const int m = 4, n = 4;
 
  // Function to flip the matrix along
  // both the diagonals
  static void flip(int[, ] matrix)
  {
    int i, j, temp;
 
    // Swapping elements
    for (i = 0; i < m / 2; i++) {
      for (j = 0; j < n; j++) {
        temp = matrix[i, j];
        matrix[i, j] = matrix[m - 1 - i, n - 1 - j];
        matrix[m - 1 - i, n - 1 - j] = temp;
      }
    }
  }
 
  // Function to print the matrix
  static void show(int[, ] matrix)
  {
    int i, j;
    for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {
        Console.Write(matrix[i, j] + "  ");
      }
      Console.WriteLine();
    }
  }
 
  // Driver Code
  public static void Main()
  {
 
    int[, ] matrix = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 } };
 
    flip(matrix);
    show(matrix);
  }
}
 
// This code is contributed by ukasp.


Javascript




<script>
        // JavaScript code for the above approach
        let m = 4, n = 4;
 
        // Function to flip the matrix along
        // both the diagonals
        function flip(matrix) {
            let i, j, temp;
 
            // Swapping elements
            for (i = 0; i < Math.floor(m / 2); i++) {
                for (j = 0; j < n; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[m - 1 - i][n - 1 - j];
                    matrix[m - 1 - i][n - 1 - j] = temp;
                }
            }
        }
 
        // Function to print the matrix
        function show(matrix) {
            let i, j;
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    document.write(matrix[i][j] + "  ");
                }
                document.write('<br>')
            }
        }
 
        // Driver Code
        let matrix = [[1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
        [13, 14, 15, 16]];
 
        flip(matrix);
        show(matrix);
 
  // This code is contributed by Potta Lokesh
    </script>


Output

16  15  14  13  
12  11  10  9  
8  7  6  5  
4  3  2  1  

Time Complexity: O(M*N)
Auxiliary Space: O(1)

 

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!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments