Rearrange given Matrix by replacing mat[i][j] with mat[ mat[i][j] ][ mat[j][i] ]
Last Updated :
19 Jan, 2022
Given a square matrix mat[][] of order N containing integers in range [0, N), the task is to rearrange the matrix elements in the given manner. Replace the element at mat[i][j] with the element at mat[x][y] where x is the number at mat[i][j] and y is the number at mat[j][i].
Examples:
Input: mat[][] = {{1, 3, 0, 4, 2},
{0, 1, 2, 3, 4},
{3, 4, 2, 1, 0},
{4, 1, 0, 2, 2},
{0, 1, 2, 1, 0}}
Output: 1 4 4 0 3
4 1 0 1 1
4 2 2 0 0
0 3 3 2 4
0 4 3 2 1
Explanation: mat[0][1] is replaced by mat[3][0] = 4, because,
x = mat[0][1] = 3, y = mat[1][0] = 0 and mat[x][y] = mat[3][0] = 4.
And similar for all other positions.
Input: mat[][] = {{0, 1, 1},
{2, 1, 2},
{0, 2, 0}}
Output: 0 2 2
2 1 0
1 0 0
Approach: The idea is to go as per the requirement of the problem. Traverse the matrix row-wise and find the element at mat[i][j] and mat[j][i]. Use these numbers x and y as the coordinates to get the replacement value for mat[i][j].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 5;
void showMatrix( int mat[][N])
{
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
cout << mat[i][j] << " " ;
}
cout << endl;
}
}
void RearrangeMatrix( int mat[][N])
{
int x, y, i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
x = mat[i][j];
y = mat[j][i];
cout << mat[x][y] << " " ;
}
cout << "\n" ;
}
}
int main()
{
int mat[][N] = { { 1, 3, 0, 4, 2 },
{ 0, 1, 2, 3, 4 },
{ 3, 4, 2, 1, 0 },
{ 4, 1, 0, 2, 2 },
{ 0, 1, 2, 1, 0 } };
RearrangeMatrix(mat);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int N = 5 ;
static void showMatrix( int mat[][])
{
int i, j;
for (i = 0 ; i < N; i++) {
for (j = 0 ; j < N; j++) {
System.out.print(mat[i][j] + " " );
}
System.out.println();
}
}
static void RearrangeMatrix( int mat[][])
{
int x, y, i, j;
for (i = 0 ; i < N; i++) {
for (j = 0 ; j < N; j++) {
x = mat[i][j];
y = mat[j][i];
System.out.print(mat[x][y] + " " );
}
System.out.println();
}
}
public static void main(String args[])
{
int mat[][] = { { 1 , 3 , 0 , 4 , 2 },
{ 0 , 1 , 2 , 3 , 4 },
{ 3 , 4 , 2 , 1 , 0 },
{ 4 , 1 , 0 , 2 , 2 },
{ 0 , 1 , 2 , 1 , 0 } };
RearrangeMatrix(mat);
}
}
|
Python3
N = 5 ;
def showMatrix(mat):
i = None
j = None
for i in range (N):
for j in range (N):
print (mat[i][j], end = " " );
print ('')
def RearrangeMatrix(mat):
x = None
y = None
i = None
j = None
for i in range (N):
for j in range (N):
x = mat[i][j];
y = mat[j][i];
print (mat[x][y], end = " " );
print ('')
mat = [[ 1 , 3 , 0 , 4 , 2 ], [ 0 , 1 , 2 , 3 , 4 ],
[ 3 , 4 , 2 , 1 , 0 ], [ 4 , 1 , 0 , 2 , 2 ],
[ 0 , 1 , 2 , 1 , 0 ]];
RearrangeMatrix(mat);
|
C#
using System;
public class GFG
{
static int N = 5;
static void showMatrix( int [, ] mat)
{
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
Console.Write(mat[i, j] + " " );
}
Console.WriteLine();
}
}
static void RearrangeMatrix( int [, ] mat)
{
int x, y, i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
x = mat[i, j];
y = mat[j, i];
Console.Write(mat[x, y] + " " );
}
Console.WriteLine();
}
}
public static void Main()
{
int [, ] mat = { { 1, 3, 0, 4, 2 },
{ 0, 1, 2, 3, 4 },
{ 3, 4, 2, 1, 0 },
{ 4, 1, 0, 2, 2 },
{ 0, 1, 2, 1, 0 } };
RearrangeMatrix(mat);
}
}
|
Javascript
<script>
let N = 5;
function showMatrix(mat) {
let i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
document.write(mat[i][j] + " " );
}
document.write( '<br>' )
}
}
function RearrangeMatrix(mat) {
let x, y, i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
x = mat[i][j];
y = mat[j][i];
document.write(mat[x][y] + " " );
}
document.write( '<br>' )
}
}
let mat = [[1, 3, 0, 4, 2],
[0, 1, 2, 3, 4],
[3, 4, 2, 1, 0],
[4, 1, 0, 2, 2],
[0, 1, 2, 1, 0]];
RearrangeMatrix(mat);
</script>
|
Output
1 4 4 0 3
4 1 0 1 1
4 2 2 0 0
0 3 3 2 4
0 4 3 2 1
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...