Open In App

Rearrange given Matrix by replacing mat[i][j] with mat[ mat[i][j] ][ mat[j][i] ]

Last Updated : 19 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to rearrange the matrix
// in specified manner.
#include <bits/stdc++.h>
using namespace std;
 
const int N = 5;
 
// Function to print matrix.
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;
    }
}
 
// Function to rearrange the matrix
// in specified manner.
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";
    }
}
 
// Driver code
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




// Java code to rearrange the matrix
// in specified manner.
import java.util.*;
public class GFG
{
 
static int N = 5;
 
// Function to print matrix.
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();
    }
}
 
// Function to rearrange the matrix
// in specified manner.
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();
    }
}
 
// Driver code
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);
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3




# Python code for the above approach
N = 5;
 
# Function to print matrix.
def showMatrix(mat):
    i = None
    j = None
    for i in range(N):
        for j in range(N):
            print(mat[i][j], end=" ");
        print('')
 
# Function to rearrange the matrix
# in specified manner.
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('')
 
# Driver code
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);
 
# This code is contributed by Saurabh Jaiswal


C#




// C# code to rearrange the matrix
// in specified manner.
using System;
public class GFG
{
 
  static int N = 5;
 
  // Function to print matrix.
  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();
    }
  }
 
  // Function to rearrange the matrix
  // in specified manner.
  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();
    }
  }
 
  // Driver code
  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);
  }
}
 
// This code is contributed by ukasp.


Javascript




<script>
      // JavaScript code for the above approach
      let N = 5;
 
      // Function to print matrix.
      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 to rearrange the matrix
      // in specified manner.
      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>')
          }
      }
 
      // Driver code
 
      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);
 
// This code is contributed by Potta Lokesh
  </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)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads