Replace the middle element of matrix with sum of surrounding elements
Last Updated :
24 Mar, 2023
Given a square matrix mat[][] of odd dimensions, the task is to change the value of the middle most element of the matrix to the sum of the elements surrounding it.
Examples:
Input:
mat[][] = { {2, 1, 7},
{3, 7, 2},
{5, 4, 9} }
Output:
2 1 7
3 10 2
5 4 9
Input:
mat[][] = {{1, 3, 5, 6, 7},
{3, 5, 3, 2, 1},
{1, 2, 3, 4, 5},
{7, 9, 2, 1, 6},
{9, 1, 5, 3, 2} }
Output:
1 3 5 6 7
3 5 3 2 1
1 2 11 4 5
7 9 2 1 6
9 1 5 3 2
Approach: The middle element of the matrix of odd order will be located at the position mat[n / 2][n / 2].
Therefore directly update the element as:
mat[n / 2][n / 2] = mat[n / 2 - 1][n / 2]
+ mat[n / 2][n / 2 - 1]
+ mat[n / 2 + 1][n / 2]
+ mat[n / 2][n / 2 + 1]
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
const int MAX = 100;
void print( int mat[][MAX], int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
cout << mat[i][j] << " " ;
}
cout << endl;
}
}
void changemiddle( int mat[][MAX], int n)
{
mat[n / 2][n / 2]
= mat[n / 2 - 1][n / 2]
+ mat[n / 2][n / 2 - 1]
+ mat[n / 2 + 1][n / 2]
+ mat[n / 2][n / 2 + 1];
print(mat, n);
}
int main()
{
int mat[][MAX] = { { 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 } };
changemiddle(mat, 3);
return 0;
}
|
C
#include <stdio.h>
#define MAX 100
void print( int mat[][MAX], int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
printf ( "%d " ,mat[i][j]);
}
printf ( "\n" );
}
}
void changemiddle( int mat[][MAX], int n)
{
mat[n / 2][n / 2]
= mat[n / 2 - 1][n / 2]
+ mat[n / 2][n / 2 - 1]
+ mat[n / 2 + 1][n / 2]
+ mat[n / 2][n / 2 + 1];
print(mat, n);
}
int main()
{
int mat[][MAX] = { { 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 } };
changemiddle(mat, 3);
return 0;
}
|
Java
public class GFG{
static void print( int mat[][], int n)
{
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
System.out.print(mat[i][j] + " " );
}
System.out.println();
}
}
static void changemiddle( int mat[][], int n)
{
mat[n / 2 ][n / 2 ]
= mat[n / 2 - 1 ][n / 2 ]
+ mat[n / 2 ][n / 2 - 1 ]
+ mat[n / 2 + 1 ][n / 2 ]
+ mat[n / 2 ][n / 2 + 1 ];
print(mat, n);
}
public static void main(String []args)
{
int mat[][] = { { 2 , 1 , 7 },
{ 3 , 7 , 2 },
{ 5 , 4 , 9 } };
changemiddle(mat, 3 );
}
}
|
Python3
MAX = 100
def printMatrix(mat, n):
for i in range (n):
for j in range (n):
print (mat[i][j], end = " " )
print ()
def changemiddle(mat, n):
mat[n / / 2 ][n / / 2 ] = (mat[n / / 2 - 1 ][n / / 2 ] +
mat[n / / 2 ][n / / 2 - 1 ] +
mat[n / / 2 + 1 ][n / / 2 ] +
mat[n / / 2 ][n / / 2 + 1 ])
printMatrix(mat, n)
if __name__ = = "__main__" :
mat = [ [ 2 , 1 , 7 ],
[ 3 , 7 , 2 ],
[ 5 , 4 , 9 ]]
changemiddle(mat, 3 )
|
C#
using System;
public class GFG{
static void print( int [,] mat, int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
Console.Write(mat[i,j] + " " );
}
Console.Write( "\n" );
}
}
static void changemiddle( int [,] mat, int n)
{
mat[n / 2,n / 2]
= mat[n / 2 - 1,n / 2]
+ mat[n / 2,n / 2 - 1]
+ mat[n / 2 + 1,n / 2]
+ mat[n / 2,n / 2 + 1];
print(mat, n);
}
public static void Main()
{
int [,] mat = { { 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 } };
changemiddle(mat, 3);
}
}
|
PHP
<?php
function printmat(& $mat , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
echo ( $mat [ $i ][ $j ]);
echo ( " " );
}
echo ( "\n" );
}
}
function changemiddle(& $mat , $n )
{
$mat [ $n / 2][ $n / 2] = $mat [ $n / 2 - 1][ $n / 2] +
$mat [ $n / 2][ $n / 2 - 1] +
$mat [ $n / 2 + 1][ $n / 2] +
$mat [ $n / 2][ $n / 2 + 1];
printmat( $mat , $n );
}
$mat = array ( array (2, 1, 7),
array (3, 7, 2),
array (5, 4, 9));
changemiddle( $mat , 3);
?>
|
Javascript
<script>
function print(mat,n)
{
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
document.write(mat[i][j] + " " );
}
document.write( "<br>" );
}
}
function changemiddle(mat,n)
{
mat[(Math.floor(n / 2))][(Math.floor(n / 2))]
= mat[(Math.floor(n / 2) - 1)][(Math.floor(n / 2))]
+ mat[(Math.floor(n / 2))][(Math.floor(n / 2) - 1)]
+ mat[(Math.floor(n / 2) + 1)][(Math.floor(n / 2))]
+ mat[(Math.floor(n / 2))][(Math.floor(n / 2) + 1)];
print(mat, n);
}
let mat = [ [ 2, 1, 7 ],
[ 3, 7, 2 ],
[ 5, 4, 9 ]] ;
changemiddle(mat, 3)
</script>
|
Output
2 1 7
3 10 2
5 4 9
Time Complexity: O(n * m), Here n is No. of Rows and m is No. of Column.
Auxiliary Space: O(1), as constant extra space is required.
Share your thoughts in the comments
Please Login to comment...