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++
#include <iostream>
using namespace std;
const int N = 3;
void makeSymmetric( int mat[][N])
{
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;
}
}
}
}
void showMatrix( int mat[][N])
{
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
cout << mat[i][j] << " " ;
}
cout << "\n" ;
}
}
int main()
{
int arr[][N]
= { { 12, 43, 65 },
{ 23, 75, 13 },
{ 51, 37, 81 } };
makeSymmetric(arr);
showMatrix(arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int N = 3 ;
static void makeSymmetric( int mat[][])
{
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 ;
}
}
}
}
static void showMatrix( int mat[][])
{
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 arr[][] = { { 12 , 43 , 65 },
{ 23 , 75 , 13 },
{ 51 , 37 , 81 } };
makeSymmetric(arr);
showMatrix(arr);
}
}
|
Python3
N = 3
def makeSymmetric(mat):
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
def showMatrix(mat):
for i in range ( 0 , N):
for j in range ( 0 , N):
print (mat[i][j], end = " " )
print ()
if __name__ = = "__main__" :
arr = [[ 12 , 43 , 65 ],
[ 23 , 75 , 13 ],
[ 51 , 37 , 81 ]]
makeSymmetric(arr)
showMatrix(arr)
|
C#
using System;
public class GFG
{
static int N = 3;
static void makeSymmetric( int [,]mat)
{
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;
}
}
}
}
static void showMatrix( int [,]mat)
{
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 [,]arr = { { 12, 43, 65 },
{ 23, 75, 13 },
{ 51, 37, 81 } };
makeSymmetric(arr);
showMatrix(arr);
}
}
|
Javascript
<script>
let N = 3;
function makeSymmetric(mat)
{
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 showMatrix(mat)
{
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
document.write(mat[i][j] + " " );
}
document.write( '<br>' )
}
}
let arr = [[12, 43, 65],
[23, 75, 13],
[51, 37, 81]];
makeSymmetric(arr);
showMatrix(arr);
</script>
|
Output
12 33 58
33 75 25
58 25 81
Time complexity: O(N2)
Space complexity: O(1)
Last Updated :
07 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...