Given a square matrix, swap the element of major and minor diagonals.
Major Diagonal Elements of a Matrix :
The Major Diagonal Elements are the ones that occur from Top Left of Matrix Down To 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 Top Right of Matrix Down To Bottom Left Corner. Also known as Secondary Diagonal.
Example :
Input : 0 1 2
3 4 5
6 7 8
Output : 2 1 0
3 4 5
8 7 6
Approach: The Simple thing one should know is that the indexes of Primary or Major diagonal are same i.e. lets say A is matrix then A[1][1] will be a Major Diagonal element and sum of indexes of Minor Diagonal is equal to size of Matrix. Lets say A is a matrix of size 3 then A[1][2] will be Minor Diagonal element.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define N 3
void swapDiagonal( int matrix[][N]) {
for ( int i = 0; i < N; i++)
swap(matrix[i][i], matrix[i][N - i - 1]);
}
int main() {
int matrix[N][N] = {{0, 1, 2},
{3, 4, 5},
{6, 7, 8}};
swapDiagonal(matrix);
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++)
cout << matrix[i][j] << " " ;
cout << endl;
}
return 0;
}
|
Java
import java.io.*;
class Gfg {
static int N = 3 ;
static void swapDiagonal( int matrix[][]) {
for ( int i = 0 ; i < N; i++) {
int temp = matrix[i][i];
matrix[i][i] = matrix[i][N - i - 1 ];
matrix[i][N - i - 1 ] = temp;
}
}
public static void main(String arg[]) {
int matrix[][] = {{ 0 , 1 , 2 },
{ 3 , 4 , 5 },
{ 6 , 7 , 8 }};
swapDiagonal(matrix);
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++)
System.out.print(matrix[i][j] + " " );
System.out.println();
}
}
}
|
Python3
N = 3
def swapDiagonal(matrix):
for i in range (N):
matrix[i][i], matrix[i][N - i - 1 ] = \
matrix[i][N - i - 1 ], matrix[i][i]
matrix = [[ 0 , 1 , 2 ],
[ 3 , 4 , 5 ],
[ 6 , 7 , 8 ]]
swapDiagonal(matrix);
for i in range (N):
for j in range (N):
print (matrix[i][j], end = ' ' )
print ()
|
C#
using System;
class Gfg {
static int N = 3;
static void swapDiagonal( int [,]matrix) {
for ( int i = 0; i < N; i++) {
int temp = matrix[i,i];
matrix[i,i] = matrix[i,N - i - 1];
matrix[i,N - i - 1] = temp;
}
}
public static void Main() {
int [,]matrix = {{0, 1, 2},
{3, 4, 5},
{6, 7, 8}};
swapDiagonal(matrix);
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++)
Console.Write(matrix[i,j] + " " );
Console.WriteLine();
}
}
}
|
PHP
<?php
$N = 3;
function swapDiagonal( $matrix )
{
global $N ;
for ( $i = 0; $i < $N ; $i ++)
{
$tmp = $matrix [ $i ][ $i ];
$matrix [ $i ][ $i ] = $matrix [ $i ][ $N - $i - 1];
$matrix [ $i ][ $N - $i - 1] = $tmp ;
}
for ( $i = 0; $i < $N ; $i ++)
{
for ( $j = 0; $j < $N ; $j ++)
echo $matrix [ $i ][ $j ] . " " ;
echo "\n" ;
}
}
$matrix = array ( array (0, 1, 2),
array (3, 4, 5),
array (6, 7, 8));
swapDiagonal( $matrix );
?>
|
Javascript
<script>
let N = 3;
function swapDiagonal(matrix) {
for (let i = 0; i < N; i++) {
let temp = matrix[i][i];
matrix[i][i] = matrix[i][N - i - 1];
matrix[i][N - i - 1] = temp;
}
}
let matrix = [[0, 1, 2],
[3, 4, 5],
[6, 7, 8]];
swapDiagonal(matrix);
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++)
document.write(matrix[i][j] + " " );
document.write( "<br/>" );
}
</script>
|
Time Complexity: O(N*N), as we are using nested loops to traverse N*N times.
Auxiliary Space: O(1), as we are not using any extra space.
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 geeks!
Last Updated :
03 Aug, 2022
Like Article
Save Article