Given a square matrix mat[][], swap the elements of its major and minor diagonals.
Major Diagonal (Primary/Main Diagonal): Elements that run from the top-left corner to the bottom-right corner of the matrix where the row and column indices are the same.
Minor Diagonal (Secondary Diagonal): Elements that run from the top-right corner to the bottom-left corner where the sum of row and column indices equals one less than the matrix size.
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
Table of Content
[Approach 1] Diagonal Buffer Swap - O(n) Time and O(n) Space
The idea is to store the elements of the major and minor diagonals separately to avoid overwriting, then swap them by placing each into the other's position.
#include <iostream>
#include <vector>
using namespace std;
void swapDiagonal(vector<vector<int>>& mat) {
int size = mat.size();
vector<int> majorDia(size), minorDia(size);
// Store major and minor diagonal elements
for (int i = 0; i < size; i++) {
majorDia[i] = mat[i][i];
minorDia[i] = mat[i][size - i - 1];
}
// Swap the diagonals by placing stored values
for (int i = 0; i < size; i++) {
mat[i][i] = minorDia[i];
mat[i][size - i - 1] = majorDia[i];
}
}
int main() {
vector<vector<int>> mat = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
};
swapDiagonal(mat);
for (const auto& row : mat) {
for (int val : row)
cout << val << " ";
cout << endl;
}
return 0;
}
class GfG {
static void swapDiagonal(int[][] mat) {
int size = mat.length;
int[] majorDia = new int[size];
int[] minorDia = new int[size];
// Store major and minor diagonal elements
for (int i = 0; i < size; i++) {
majorDia[i] = mat[i][i];
minorDia[i] = mat[i][size - i - 1];
}
// Swap the diagonals by placing stored values
for (int i = 0; i < size; i++) {
mat[i][i] = minorDia[i];
mat[i][size - i - 1] = majorDia[i];
}
}
public static void main(String[] args) {
int[][] mat = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
};
swapDiagonal(mat);
for (int[] row : mat) {
for (int val : row)
System.out.print(val + " ");
System.out.println();
}
}
}
def swapDiagonal(mat):
size = len(mat)
majorDia = [0] * size
minorDia = [0] * size
# Store major and minor diagonal elements
for i in range(size):
majorDia[i] = mat[i][i]
minorDia[i] = mat[i][size - i - 1]
# Swap the diagonals by placing stored values
for i in range(size):
mat[i][i] = minorDia[i]
mat[i][size - i - 1] = majorDia[i]
if __name__ == "__main__":
mat = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
swapDiagonal(mat)
for row in mat:
print(*row)
using System;
class GfG {
static void swapDiagonal(int[,] mat) {
int size = mat.GetLength(0);
int[] majorDia = new int[size];
int[] minorDia = new int[size];
// Store major and minor diagonal elements
for (int i = 0; i < size; i++) {
majorDia[i] = mat[i, i];
minorDia[i] = mat[i, size - i - 1];
}
// Swap the diagonals by placing stored values
for (int i = 0; i < size; i++) {
mat[i, i] = minorDia[i];
mat[i, size - i - 1] = majorDia[i];
}
}
static void Main() {
int[,] mat = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
};
swapDiagonal(mat);
int size = mat.GetLength(0);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
Console.Write(mat[i, j] + " ");
}
Console.WriteLine();
}
}
}
function swapDiagonal(mat) {
const size = mat.length;
const majorDia = new Array(size);
const minorDia = new Array(size);
// Store major and minor diagonal elements
for (let i = 0; i < size; i++) {
majorDia[i] = mat[i][i];
minorDia[i] = mat[i][size - i - 1];
}
// Swap the diagonals by placing stored values
for (let i = 0; i < size; i++) {
mat[i][i] = minorDia[i];
mat[i][size - i - 1] = majorDia[i];
}
}
// Driver Code
const mat = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
];
swapDiagonal(mat);
for (let row of mat) {
console.log(row.join(' '));
}
Output
2 1 0 3 4 5 8 7 6
[Approach 2] Two-Pointer Diagonal Swap - O(n) Time and O(1) Time
In a square matrix, the major diagonal consists of elements where the row and column indices are equal (i.e., mat[i][i]), while the minor diagonal has elements where the sum of row and column indices equals one less than the matrix size (i.e., mat[i][mat.size() - 1 - i]).
To swap these diagonals, iterate through each row using a two-pointer like logic: one pointer accesses the major diagonal element, and the other accesses the minor diagonal element. Swap these elements in each row.

#include <iostream>
#include <vector>
using namespace std;
void swapDiagonal(vector<vector<int>>& mat) {
int n = mat.size();
for (int i = 0; i < n; i++) {
// swap major and minor diagonal elements.
swap(mat[i][i], mat[i][n - i - 1]);
}
}
int main() {
vector<vector<int>> mat =
{{0, 1, 2},
{3, 4, 5},
{6, 7, 8}};
swapDiagonal(mat);
for (int i = 0; i < mat.size(); i++) {
for (int j = 0; j < mat.size(); j++)
cout << mat[i][j] << " ";
cout << endl;
}
return 0;
}
class GfG {
static void swapDiagonal(int[][] mat) {
int size = mat.length;
for (int i = 0; i < size; i++) {
// Swap major and minor diagonal elements
int temp = mat[i][i];
mat[i][i] = mat[i][size - i - 1];
mat[i][size - i - 1] = temp;
}
}
public static void main(String[] args) {
int[][] mat = {{0, 1, 2},
{3, 4, 5},
{6, 7, 8}};
swapDiagonal(mat);
for (int[] row : mat) {
for (int val : row)
System.out.print(val + " ");
System.out.println();
}
}
}
def swapDiagonal(mat):
size = len(mat)
for i in range(size):
# Swap major and minor diagonal elements
mat[i][i], mat[i][size - i - 1] = mat[i][size - i - 1], mat[i][i]
if __name__ == "__main__":
mat = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
swapDiagonal(mat)
for row in mat:
print(*row)
using System;
class GfG {
static void swapDiagonal(int[,] mat) {
int size = mat.GetLength(0);
for (int i = 0; i < size; i++) {
// Swap major and minor diagonal elements
int temp = mat[i, i];
mat[i, i] = mat[i, size - i - 1];
mat[i, size - i - 1] = temp;
}
}
static void Main() {
int[,] mat = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
};
swapDiagonal(mat);
int size = mat.GetLength(0);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
Console.Write(mat[i, j] + " ");
}
Console.WriteLine();
}
}
}
function swapDiagonal(mat) {
const size = mat.length;
for (let i = 0; i < size; i++) {
// Swap major and minor diagonal elements
let temp = mat[i][i];
mat[i][i] = mat[i][size - i - 1];
mat[i][size - i - 1] = temp;
}
}
// Driver Code
const mat = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
];
swapDiagonal(mat);
for (let row of mat) {
console.log(row.join(' '));
}
Output
2 1 0 3 4 5 8 7 6