The Wayback Machine - https://web.archive.org/web/20250317143336/https://www.geeksforgeeks.org/swap-major-minor-diagonals-square-matrix/
Open In App

Swap major and minor diagonals of a square matrix

Last Updated : 21 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a square matrix mat[][] of order n*n, the task is to swap the elements of major and minor diagonals.

The major and minor diagonal matrix explanation is given below:

  • Major Diagonal Elements of a Matrix: The Major Diagonal Elements are the ones that occur from the Top Left of the Matrix Down To the Bottom Right Corner. The Major Diagonal is also known as Main Diagonal or Primary Diagonal.
  • Minor Diagonal Elements of a Matrix: The Minor Diagonal Elements are the ones that occur from the Top Right of the Matrix Down To the Bottom Left Corner. Also known as Secondary Diagonal.

Examples: 

Input:
mat[][] = [[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]

Output : [[2, 1, 0],
[3, 4, 5],
[8, 7, 6]]

Explanation: Major Diagonal = {0, 4, 8}, Minor Diagonal = {2, 4, 6}. We are required to swap the diagonal elements of same row, thus after doing so, major diagonal will become minor and vice-versa.

Input:
mat[][] = [[2, 3],
[5, 4]]

Output : [[3, 2],
[4, 5]]

Explanation: Major Diagonal = {2, 4}, Minor Diagonal = {3, 5}. We are required to swap the diagonal elements of same row, thus after doing so, major diagonal will become minor and vice-versa

Approach:

The row and column indexes of Primary or Major diagonal are same i.e. lets say mat[][] is matrix then mat[i][i] will be a Major Diagonal element. And sum of row and column indexes of Minor or Secondary Diagonal is equal to count of columns in a Matrix. Lets say mat[][] is a matrix with n columns then mat[i][n-i-1] will be Minor Diagonal element. The idea is to interchange these two indexes of each row in a matrix and return the resultant matrix.

C++
// CPP Program to swap diagonal of a matrix
#include <bits/stdc++.h>
using namespace std;

// Function to swap diagonal of matrix
void swapDiagonal(vector<vector<int>> &matrix) {

    int n = matrix.size();
    for (int i = 0; i < n; i++)
        
        // swap major and minor diagonal elements.
        swap(matrix[i][i], matrix[i][n - i - 1]);
}

// Function to display matrix
void displayMatrix(vector<vector<int>> &matrix) {

    for (int i = 0; i < matrix.size(); i++) {
        for (int j = 0; j < matrix.size(); j++)
            cout << matrix[i][j] << " ";
        cout << endl;
    }
}

int main() {
    vector<vector<int>> matrix = {{0, 1, 2},
                        {3, 4, 5},
                        {6, 7, 8}};

    swapDiagonal(matrix);
    displayMatrix(matrix);
    return 0;
}
C
// C program to swap diagonal of a matrix
#include <stdio.h>

// Function to swap diagonal of matrix
void swapDiagonal(int matrix[3][3], int n) {
    for (int i = 0; i < n; i++) {
        
        // swap major and minor diagonal elements
        int temp = matrix[i][i];
        matrix[i][i] = matrix[i][n - i - 1];
        matrix[i][n - i - 1] = temp;
    }
}

// Function to display matrix
void displayMatrix(int matrix[3][3], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int matrix[3][3] = {
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8}
    };

    int n = 3;

    swapDiagonal(matrix, n);
    displayMatrix(matrix, n);

    return 0;
}
Java
// Java Program to swap diagonals of a matrix using a 2D array
import java.util.*;

class GfG {

    // Function to swap diagonal elements of the matrix
    static void swapDiagonal(int[][] matrix) {

        int n = matrix.length;
        for (int i = 0; i < n; i++) {
          
            // Swap major and minor diagonal elements.
            int temp = matrix[i][i];
            matrix[i][i] = matrix[i][n - i - 1];
            matrix[i][n - i - 1] = temp;
        }
    }

    // Function to display the matrix
    static void displayMatrix(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
      
        int[][] matrix = {
            {0, 1, 2},
            {3, 4, 5},
            {6, 7, 8}
        };

        swapDiagonal(matrix);
        displayMatrix(matrix);
    }
}
Python
# Python program to swap diagonal of a matrix

# Function to swap diagonal of matrix
def swapDiagonal(matrix):
    n = len(matrix)
    for i in range(n):

        # swap major and minor diagonal elements.
        matrix[i][i], matrix[i][n - i - 1] = matrix[i][n - i - 1], matrix[i][i]

# Function to display matrix

def displayMatrix(matrix):
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            print(matrix[i][j], end=" ")
        print()


if __name__ == "__main__":
    matrix = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]
    ]

    swapDiagonal(matrix)
    displayMatrix(matrix)
C#
// C# program to swap diagonal of a matrix
using System;

class GfG {

    // Function to swap diagonal of matrix
    static void swapDiagonal(int[, ] matrix) {
        int n = matrix.GetLength(0);
        for (int i = 0; i < n; i++) {

            // swap major and minor diagonal elements
            int temp = matrix[i, i];
            matrix[i, i] = matrix[i, n - i - 1];
            matrix[i, n - i - 1] = temp;
        }
    }

    // Function to display matrix
    static void displayMatrix(int[, ] matrix) {
        int n = matrix.GetLength(0);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                Console.Write(matrix[i, j] + " ");
            Console.WriteLine();
        }
    }

    static void Main(string[] args) {
        int[, ] matrix
            = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };

        swapDiagonal(matrix);
        displayMatrix(matrix);
    }
}
JavaScript
// JavaScript program to swap diagonal of a matrix

// Function to swap diagonal of matrix
function swapDiagonal(matrix) {

    const n = matrix.length;
    for (let i = 0; i < n; i++) {

        // swap major and minor diagonal elements
        const temp = matrix[i][i];
        matrix[i][i] = matrix[i][n - i - 1];
        matrix[i][n - i - 1] = temp;
    }
}

// Function to display matrix
function displayMatrix(matrix) {
    const n = matrix.length;
    for (let i = 0; i < n; i++) {
        let row = "";
        for (let j = 0; j < n; j++) {
            row += matrix[i][j] + " ";
        }
        console.log(row.trim());
    }
}

//driver code
const matrix = [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ] ];
swapDiagonal(matrix);
displayMatrix(matrix);

Output
2 1 0 
3 4 5 
8 7 6 

Time Complexity: O(n*n), to traverse the matrix while printing every element.
Auxiliary Space: O(1)



Next Article

Similar Reads

three90RightbarBannerImg