Mirror of matrix across diagonal
Last Updated :
25 Jul, 2022
Given a 2-D array of order N x N, print a matrix that is the mirror of the given tree across the diagonal. We need to print the result in a way: swap the values of the triangle above the diagonal with the values of the triangle below it like a mirror image swap. Print the 2-D array obtained in a matrix layout.
Examples:
Input : int mat[][] = {{1 2 4 }
{5 9 0}
{ 3 1 7}}
Output : 1 5 3
2 9 1
4 0 7
Input : mat[][] = {{1 2 3 4 }
{5 6 7 8 }
{9 10 11 12}
{13 14 15 16} }
Output : 1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
A simple solution to this problem involves extra space. We traverse all right diagonal (right-to-left) one by one. During the traversal of the diagonal, first, we push all the elements into the stack and then we traverse it again and replace every element of the diagonal with the stack element.
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
void imageSwap( int mat[][MAX], int n)
{
int row = 0;
for ( int j = 0; j < n; j++) {
stack< int > s;
int i = row, k = j;
while (i < n && k >= 0)
s.push(mat[i++][k--]);
i = row, k = j;
while (i < n && k >= 0) {
mat[i++][k--] = s.top();
s.pop();
}
}
int column = n - 1;
for ( int j = 1; j < n; j++) {
stack< int > s;
int i = j, k = column;
while (i < n && k >= 0)
s.push(mat[i++][k--]);
i = j;
k = column;
while (i < n && k >= 0) {
mat[i++][k--] = s.top();
s.pop();
}
}
}
void printMatrix( int mat[][MAX], int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++)
cout << mat[i][j] << " " ;
cout << endl;
}
}
int main()
{
int mat[][MAX] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int n = 4;
imageSwap(mat, n);
printMatrix(mat, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MAX = 100 ;
static void imageSwap( int mat[][], int n)
{
int row = 0 ;
for ( int j = 0 ; j < n; j++)
{
Stack<Integer> s = new Stack<>();
int i = row, k = j;
while (i < n && k >= 0 )
{
s.push(mat[i++][k--]);
}
i = row;
k = j;
while (i < n && k >= 0 )
{
mat[i++][k--] = s.peek();
s.pop();
}
}
int column = n - 1 ;
for ( int j = 1 ; j < n; j++)
{
Stack<Integer> s = new Stack<>();
int i = j, k = column;
while (i < n && k >= 0 )
{
s.push(mat[i++][k--]);
}
i = j;
k = column;
while (i < n && k >= 0 )
{
mat[i++][k--] = s.peek();
s.pop();
}
}
}
static void printMatrix( int mat[][], int n)
{
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
System.out.print(mat[i][j] + " " );
}
System.out.println( "" );
}
}
public static void main(String[] args)
{
int mat[][] = {{ 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 }};
int n = 4 ;
imageSwap(mat, n);
printMatrix(mat, n);
}
}
|
Python3
MAX = 100
def imageSwap(mat, n):
row = 0
for j in range (n):
s = []
i = row
k = j
while (i < n and k > = 0 ):
s.append(mat[i][k])
i + = 1
k - = 1
i = row
k = j
while (i < n and k > = 0 ):
mat[i][k] = s[ - 1 ]
k - = 1
i + = 1
s.pop()
column = n - 1
for j in range ( 1 , n):
s = []
i = j
k = column
while (i < n and k > = 0 ):
s.append(mat[i][k])
i + = 1
k - = 1
i = j
k = column
while (i < n and k > = 0 ):
mat[i][k] = s[ - 1 ]
i + = 1
k - = 1
s.pop()
def printMatrix(mat, n):
for i in range (n):
for j in range (n):
print (mat[i][j], end = " " )
print ()
mat = [[ 1 , 2 , 3 , 4 ],[ 5 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],[ 13 , 14 , 15 , 16 ]]
n = 4
imageSwap(mat, n)
printMatrix(mat, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MAX = 100;
static void imageSwap( int [,]mat, int n)
{
int row = 0;
for ( int j = 0; j < n; j++)
{
Stack< int > s = new Stack< int >();
int i = row, k = j;
while (i < n && k >= 0)
{
s.Push(mat[i++,k--]);
}
i = row;
k = j;
while (i < n && k >= 0)
{
mat[i++,k--] = s.Peek();
s.Pop();
}
}
int column = n - 1;
for ( int j = 1; j < n; j++)
{
Stack< int > s = new Stack< int >();
int i = j, k = column;
while (i < n && k >= 0)
{
s.Push(mat[i++,k--]);
}
i = j;
k = column;
while (i < n && k >= 0)
{
mat[i++,k--] = s.Peek();
s.Pop();
}
}
}
static void printMatrix( int [,]mat, int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
Console.Write(mat[i,j] + " " );
}
Console.WriteLine( "" );
}
}
public static void Main(String[] args)
{
int [,]mat = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int n = 4;
imageSwap(mat, n);
printMatrix(mat, n);
}
}
|
Javascript
<script>
let MAX = 100;
function imageSwap(mat, n)
{
let row = 0;
for (let j = 0; j < n; j++)
{
let s = [];
let i = row, k = j;
while (i < n && k >= 0)
{
s.push(mat[i++][k--]);
}
i = row;
k = j;
while (i < n && k >= 0)
{
mat[i++][k--] = s[s.length - 1];
s.pop();
}
}
let column = n - 1;
for (let j = 1; j < n; j++)
{
let s = [];
let i = j, k = column;
while (i < n && k >= 0)
{
s.push(mat[i++][k--]);
}
i = j;
k = column;
while (i < n && k >= 0)
{
mat[i++][k--] = s[s.length - 1];
s.pop();
}
}
}
function printMatrix(mat, n)
{
for (let i = 0; i < n; i++)
{
for (let j = 0; j < n; j++)
{
document.write(mat[i][j] + " " );
}
document.write( "</br>" );
}
}
let mat = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]];
let n = 4;
imageSwap(mat, n);
printMatrix(mat, n);
</script>
|
Output:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Time complexity : O(n2)
Auxiliary Space: O(n), as stack is used
An efficient solution to this problem is that if we observe an output matrix, then we notice that we just have to swap (mat[i][j] to mat[j][i]).
Below is the implementation of the above idea.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
void imageSwap( int mat[][MAX], int n)
{
for ( int i = 0; i < n; i++)
for ( int j = 0; j <= i; j++)
mat[i][j] = mat[i][j] + mat[j][i] -
(mat[j][i] = mat[i][j]);
}
void printMatrix( int mat[][MAX], int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++)
cout << mat[i][j] << " " ;
cout << endl;
}
}
int main()
{
int mat[][MAX] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int n = 4;
imageSwap(mat, n);
printMatrix(mat, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int MAX = 100 ;
static void imageSwap( int mat[][], int n)
{
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j <= i; j++)
mat[i][j] = mat[i][j] + mat[j][i]
- (mat[j][i] = mat[i][j]);
}
static void printMatrix( int mat[][], int n)
{
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++)
System.out.print( mat[i][j] + " " );
System.out.println();
}
}
public static void main (String[] args)
{
int mat[][] = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 } };
int n = 4 ;
imageSwap(mat, n);
printMatrix(mat, n);
}
}
|
Python3
from builtins import range
MAX = 100 ;
def imageSwap(mat, n):
for i in range (n):
for j in range (i + 1 ):
t = mat[i][j];
mat[i][j] = mat[j][i]
mat[j][i] = t
def printMatrix(mat, n):
for i in range (n):
for j in range (n):
print (mat[i][j], end = " " );
print ();
if __name__ = = '__main__' :
mat = [ 1 , 2 , 3 , 4 ], \
[ 5 , 6 , 7 , 8 ], \
[ 9 , 10 , 11 , 12 ], \
[ 13 , 14 , 15 , 16 ];
n = 4 ;
imageSwap(mat, n);
printMatrix(mat, n);
|
C#
using System;
class GFG {
static void imageSwap( int [,]mat, int n)
{
for ( int i = 0; i < n; i++)
for ( int j = 0; j <= i; j++)
mat[i,j] = mat[i,j] + mat[j,i]
- (mat[j,i] = mat[i,j]);
}
static void printMatrix( int [,]mat, int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++)
Console.Write( mat[i,j] + " " );
Console.WriteLine();
}
}
public static void Main ()
{
int [,]mat = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int n = 4;
imageSwap(mat, n);
printMatrix(mat, n);
}
}
|
PHP
<?php
function imageSwap(& $mat , $n )
{
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j <= $i ; $j ++)
$mat [ $i ][ $j ] = $mat [ $i ][ $j ] + $mat [ $j ][ $i ] -
( $mat [ $j ][ $i ] = $mat [ $i ][ $j ]);
}
function printMatrix(& $mat , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
echo ( $mat [ $i ][ $j ]);
echo ( " " );
}
echo ( "\n" );
}
}
$mat = array ( array (1, 2, 3, 4),
array (5, 6, 7, 8),
array (9, 10, 11, 12),
array (13, 14, 15, 16));
$n = 4;
imageSwap( $mat , $n );
printMatrix( $mat , $n );
?>
|
Javascript
<script>
let MAX = 100;
function imageSwap(mat, n)
{
for (let i = 0; i < n; i++)
for (let j = 0; j <= i; j++)
mat[i][j] = mat[i][j] + mat[j][i]
- (mat[j][i] = mat[i][j]);
}
function printMatrix(mat, n)
{
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++)
document.write(mat[i][j] + " " );
document.write( "</br>" );
}
}
let mat = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ] ];
let n = 4;
imageSwap(mat, n);
printMatrix(mat, n);
</script>
|
Output:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Time complexity : O(n2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...