Given a square matrix mat[][] of dimensions N * N, the task is to print the matrix that can be obtained after swapping the laterally inverted images of the upper and lower triangular halves of a given matrix.
Consider the matrix mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
The lateral image of the lower triangular half of the matrix
4
7 8
The lateral image of the upper triangular half of the matrix
6
3 2
Therefore, following rearrangement of the matrix needs to be performed
1 2 3 1 8 7
4 5 6 to 6 5 4
7 8 9 3 2 9
Examples:
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output:
1 8 7
6 5 4
3 2 9
Explanation:
1 2 3 6 5 4
1 8 7 to 7 8 9
4 5 6 3 2 9
Input: mat[][] = {{1, 2}, {4, 5}}
Output:
1 4
2 5
Approach: Follow the steps below to solve the problem:
- Initialize an array of vectors, upDiagonal, and lowDiagonal, to store the elements of the matrix elements from the lower and upper triangular halves respectively.
- Traverse the given matrix using variables i and j for rows and column respectively and perform the following steps:
- If the current element is on the principal diagonal, then continue from this iteration.
- Otherwise, if the current element is present in the upper triangular half, then add this element to upDiagonal at index abs(i – j).
- Otherwise, add the current element to lowDiagonal at index abs(i – j).
- Now, again traverse the matrix and replace any element present in the upper-half with the element from the end of the lower-half and vice versa.
- After completing the above steps, print the resultant matrix.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void ReverseSwap(vector<vector< int > >& mat, int n)
{
vector< int > lowerEle[n];
vector< int > upperEle[n];
int index;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
index = abs (i - j);
if (i == j) {
continue ;
}
else if (j < i) {
lowerEle[index].push_back(
mat[i][j]);
}
else {
upperEle[index].push_back(
mat[i][j]);
}
}
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
index = abs (i - j);
if (i == j) {
continue ;
}
else if (j < i) {
mat[i][j] = upperEle[index].back();
upperEle[index].pop_back();
}
else {
mat[i][j] = lowerEle[index].back();
lowerEle[index].pop_back();
}
}
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
cout << mat[i][j] << " " ;
}
cout << endl;
}
}
int main()
{
vector<vector< int > > mat = { { 1, 2 },
{ 4, 5 } };
int N = mat.size();
ReverseSwap(mat, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void ReverseSwap( int [][] mat, int n)
{
int [] lowerEle = new int [n];
int [] upperEle = new int [n];
int index;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
index = Math.abs(i - j);
if (i == j)
{
continue ;
}
else if (j < i)
{
lowerEle[index] = mat[i][j];
}
else
{
upperEle[index] = mat[i][j];
}
}
}
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
index = Math.abs(i - j);
if (i == j)
{
continue ;
}
else if (j < i)
{
mat[i][j] = upperEle[index];
}
else
{
mat[i][j] = lowerEle[index--];
}
}
}
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 = new int [][]{ { 1 , 2 }, { 4 , 5 } };
int N = mat.length;
ReverseSwap(mat, N);
}
}
|
Python3
def ReverseSwap(mat, n):
lowerEle = [[] for i in range (n)]
upperEle = [[] for i in range (n)]
index = 0
for i in range (n):
for j in range (n):
index = abs (i - j)
if (i = = j):
continue
elif (j < i):
lowerEle[index].append(mat[i][j])
else :
upperEle[index].append(mat[i][j])
for i in range (n):
for j in range (n):
index = abs (i - j)
if (i = = j):
continue
elif (j < i):
mat[i][j] = upperEle[index][ - 1 ]
del upperEle[index][ - 1 ]
else :
mat[i][j] = lowerEle[index][ - 1 ]
del lowerEle[index][ - 1 ]
for i in range (n):
for j in range (n):
print (mat[i][j], end = " " )
print ()
if __name__ = = '__main__' :
mat = [ [ 1 , 2 ],
[ 4 , 5 ] ]
N = len (mat)
ReverseSwap(mat, N)
|
C#
using System;
class GFG{
static void ReverseSwap( int [,] mat, int n)
{
int [] lowerEle = new int [n];
int [] upperEle = new int [n];
int index;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
index = Math.Abs(i - j);
if (i == j)
{
continue ;
}
else if (j < i)
{
lowerEle[index] = mat[i, j];
}
else
{
upperEle[index] = mat[i, j];
}
}
}
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
index = Math.Abs(i - j);
if (i == j)
{
continue ;
}
else if (j < i)
{
mat[i, j] = upperEle[index];
}
else
{
mat[i, j] = lowerEle[index--];
}
}
}
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
Console.Write(mat[i, j] + " " );
}
Console.WriteLine();
}
}
static public void Main()
{
int [,] mat = new int [,]{ { 1, 2 }, { 4, 5 } };
int N = mat.GetLength(0);
ReverseSwap(mat, N);
}
}
|
Javascript
<script>
function ReverseSwap(mat,n)
{
let lowerEle = new Array(n);
let upperEle = new Array(n);
let index;
for (let i = 0; i < n; i++)
{
for (let j = 0; j < n; j++)
{
index = Math.abs(i - j);
if (i == j)
{
continue ;
}
else if (j < i)
{
lowerEle[index] = mat[i][j];
}
else
{
upperEle[index] = mat[i][j];
}
}
}
for (let i = 0; i < n; i++)
{
for (let j = 0; j < n; j++)
{
index = Math.abs(i - j);
if (i == j)
{
continue ;
}
else if (j < i)
{
mat[i][j] = upperEle[index];
}
else
{
mat[i][j] = lowerEle[index--];
}
}
}
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],[ 4, 5 ]];
let N = mat.length;
ReverseSwap(mat, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Optimised Approach: Without space
We will traverse only the upper triangular half and swap elements of the upper triangular half with the lower triangular half. But how we can access elements of the lower triangular half if we are only traversing the upper triangular half?
Because if the index of any element in the upper triangular half is “i,j” then “j,i”will be the index of the corresponding element in the lower triangular half.
Code-
C++
#include <bits/stdc++.h>
using namespace std;
void swapUpperToLower(vector<vector< int > > mat, int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++)
cout << mat[i][j] << " " ;
cout << endl;
}
}
int main()
{
vector<vector< int > > mat = { { 1, 2 },
{ 4, 5 } };
int n = mat.size();
swapUpperToLower(mat,n);
return 0;
}
|
Output-
1 4
2 5
Time Complexity: O(N2)
Auxiliary Space: O(1)