Convert given Matrix into a Symmetric Matrix by replacing elements at (i, j) and (j, i) with their mean

Given an integer N and a N x N matrix, the task is to convert the given matrix into a symmetric matrix by replacing (i, j)th and (j, i)th element with their arithmetic mean.
Examples:
Input: arr[] = {{1, 2, 3},Â
            {4, 5, 6},Â
            {7, 8, 9}}
Output:Â
1 3 5
3 5 7
5 7 9
Explanation: The diagonal elements are same. The element at index (0, 1) = 2 and (1, 0) = 4 is replaced by their arithmetic mean i.e, (2 + 4) / 2 = 3. Similarly, the elements at index (2, 0) and (0, 2), (2, 1) and (1, 2) are also replaced by their arithmetic mean and the resulting output matrix is a symmetric matrix.Input: arr[] = {{12, 43, 65},Â
            {23, 75, 13},Â
            {51, 37, 81}}
Output:
12 33 58
33 75 25
58 25 81
Approach: The given problem is an implementation-based problem. The idea is to traverse the lower triangular matrix and replace the elements and their respective transpose indices with their arithmetic mean.Â
Below is the implementation of the above approach:
C++
// C++ program of the above approach#include <iostream>using namespace std;const int N = 3;Â
// Function to convert the given matrix// into a symmetric matrix by replacing// transpose elements with their meanvoid makeSymmetric(int mat[][N]){    // Loop to traverse lower triangular    // elements of the given matrix    for (int i = 0; i < N; i++) {        for (int j = 0; j < N; j++) {            if (j < i) {                mat[i][j] = mat[j][i]                    = (mat[i][j] +                        mat[j][i]) / 2;            }        }    }}Â
// Function to print the given matrixvoid showMatrix(int mat[][N]){    // Loop to traverse the    // given matrix    for (int i = 0; i < N; i++) {        for (int j = 0; j < N; j++) {Â
            // Print current index            cout << mat[i][j] << " ";        }        cout << "\n";    }}Â
// Driver Codeint main(){    int arr[][N]        = { { 12, 43, 65 },            { 23, 75, 13 },            { 51, 37, 81 } };Â
    makeSymmetric(arr);    showMatrix(arr);Â
    return 0;} |
Java
// Java program of the above approachimport java.util.*;Â
class GFG{Â
static int N = 3;Â
// Function to convert the given matrix// into a symmetric matrix by replacing// transpose elements with their meanstatic void makeSymmetric(int mat[][]){         // Loop to traverse lower triangular    // elements of the given matrix    for(int i = 0; i < N; i++)     {        for(int j = 0; j < N; j++)         {            if (j < i)            {                mat[i][j] = mat[j][i] = (mat[i][j] +                                          mat[j][i]) / 2;            }        }    }}Â
// Function to print the given matrixstatic void showMatrix(int mat[][]){         // Loop to traverse the    // given matrix    for(int i = 0; i < N; i++)    {        for(int j = 0; j < N; j++)        {                         // Print current index            System.out.print(mat[i][j] + " ");        }        System.out.println();    }} Â
// Driver Codepublic static void main(String args[]){    int arr[][] = { { 12, 43, 65 },                    { 23, 75, 13 },                    { 51, 37, 81 } };Â
    makeSymmetric(arr);    showMatrix(arr);}}Â
// This code is contributed by sanjoy_62 |
Python3
# python3 program of the above approachN = 3Â
# Function to convert the given matrix# into a symmetric matrix by replacing# transpose elements with their meandef makeSymmetric(mat):Â
    # Loop to traverse lower triangular    # elements of the given matrix    for i in range(0, N):        for j in range(0, N):            if (j < i):                mat[i][j] = mat[j][i] = (mat[i][j] +                                         mat[j][i]) // 2Â
# Function to print the given matrixdef showMatrix(mat):Â
    # Loop to traverse the    # given matrix    for i in range(0, N):        for j in range(0, N):Â
            # Print current index            print(mat[i][j], end=" ")Â
        print()Â
# Driver Codeif __name__ == "__main__":Â
    arr = [[12, 43, 65],           [23, 75, 13],           [51, 37, 81]]Â
    makeSymmetric(arr)    showMatrix(arr)Â
# This code is contributed by rakeshsahni |
C#
// C# program of the above approachusing System;public class GFG{Â
  static int N = 3;Â
  // Function to convert the given matrix  // into a symmetric matrix by replacing  // transpose elements with their mean  static void makeSymmetric(int [,]mat)  {Â
    // Loop to traverse lower triangular    // elements of the given matrix    for(int i = 0; i < N; i++)     {      for(int j = 0; j < N; j++)       {        if (j < i)        {          mat[i,j] = mat[j,i] = (mat[i,j] +                                  mat[j,i]) / 2;        }      }    }  }Â
  // Function to print the given matrix  static void showMatrix(int [,]mat)  {Â
    // Loop to traverse the    // given matrix    for(int i = 0; i < N; i++)    {      for(int j = 0; j < N; j++)      {Â
        // Print current index        Console.Write(mat[i, j] + " ");      }      Console.WriteLine();    }  } Â
  // Driver Code  public static void Main(String []args)  {    int [,]arr = { { 12, 43, 65 },                  { 23, 75, 13 },                  { 51, 37, 81 } };Â
    makeSymmetric(arr);    showMatrix(arr);  }}// This code is contributed by 29AjayKumar |
Javascript
  <script>      // JavaScript code for the above approach      let N = 3;Â
      // Function to convert the given matrix      // into a symmetric matrix by replacing      // transpose elements with their mean      function makeSymmetric(mat)      {                 // Loop to traverse lower triangular          // elements of the given matrix          for (let i = 0; i < N; i++) {              for (let j = 0; j < N; j++) {                  if (j < i) {                      mat[i][j] = mat[j][i]                          = Math.floor((mat[i][j] +                              mat[j][i]) / 2);                  }              }          }      }Â
      // Function to print the given matrix      function showMatrix(mat)      {                 // Loop to traverse the          // given matrix          for (let i = 0; i < N; i++) {              for (let j = 0; j < N; j++) {Â
                  // Print current index                  document.write(mat[i][j] + " ");              }              document.write('<br>')          }      }Â
      // Driver Code      let arr = [[12, 43, 65],          [23, 75, 13],          [51, 37, 81]];Â
      makeSymmetric(arr);      showMatrix(arr);       // This code is contributed by Potta Lokesh  </script> |
12 33 58 33 75 25 58 25 81
Time complexity: O(N2)
Space complexity: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



