Filling diagonal to make the sum of every row, column and diagonal equal of 3×3 matrix
Given 9 elements in a 3 x 3 matrix where the value of diagonals is 0. We need to find the values in the diagonal to make the sum of every row, column, and diagonal equal.
Examples:
Input:
0 3 6
5 0 5
4 7 0
Output:
6 3 6
5 5 5
4 7 4
Explanation:
Now the value of the sum of
any row or column is 15
Input:
0 4 4
4 0 4
4 4 0
Output:
4 4 4
4 4 4
4 4 4
Approach:
- Let’s say the diagonal is x, y and z.
- The value of x will be ( x2, 3 + x3, 2 ) / 2.
- The value of z will be ( x1, 2 + x2, 1 ) / 2.
- The value of y will be ( x + z ) / 2.
Below is the implementation of the above approach:
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void print( int arr[3][3])
{
int i = 0, j = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
cout << arr[i][j] << " " ;
cout << endl;
}
}
void find( int arr[3][3])
{
arr[0][0] = (arr[1][2] + arr[2][1]) / 2;
arr[2][2] = (arr[0][1] + arr[1][0]) / 2;
arr[1][1] = (arr[0][0] + arr[1][1]) / 2;
cout << "Matrix with diagonals:\n" ;
print(arr);
}
int main()
{
int arr[3][3] = { { 0, 54, 48 },
{ 36, 0, 78 },
{ 66, 60, 0 } };
cout << "Matrix initially:\n" ;
print(arr);
find(arr);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static void print( int arr[][])
{
int i = 0 , j = 0 ;
for (i = 0 ; i < 3 ; i++)
{
for (j = 0 ; j < 3 ; j++)
System.out.print( arr[i][j] + " " );
System.out.println();
}
}
static void find( int arr[][])
{
arr[ 0 ][ 0 ] = (arr[ 1 ][ 2 ] + arr[ 2 ][ 1 ]) / 2 ;
arr[ 2 ][ 2 ] = (arr[ 0 ][ 1 ] + arr[ 1 ][ 0 ]) / 2 ;
arr[ 1 ][ 1 ] = (arr[ 0 ][ 0 ] + arr[ 1 ][ 1 ]) / 2 ;
System.out.print( "Matrix with diagonals:\n" );
print(arr);
}
public static void main(String args[])
{
int arr[][] = { { 0 , 54 , 48 },
{ 36 , 0 , 78 },
{ 66 , 60 , 0 } };
System.out.print( "Matrix initially:\n" );
print(arr);
find(arr);
}
}
|
Python3
def print_(arr, n):
for i in range (n):
for j in range (n):
print (arr[i][j], end = " " )
print ( "\n" , end = "")
def find(arr, n):
arr[ 0 ][ 0 ] = (arr[ 1 ][ 2 ] + arr[ 2 ][ 1 ]) / / 2
arr[ 2 ][ 2 ] = (arr[ 0 ][ 1 ] + arr[ 1 ][ 0 ]) / / 2
arr[ 1 ][ 1 ] = (arr[ 0 ][ 0 ] + arr[ 1 ][ 1 ]) / / 2
print ( "\nMatrix with diagonals:" )
print_(arr, n)
arr = [[ 0 , 54 , 48 ],
[ 36 , 0 , 78 ],
[ 66 , 60 , 0 ]]
n = 3
print ( "Matrix initially:" )
print_(arr, n)
find(arr, n)
|
C#
using System;
class GFG
{
static void print( int [,]arr)
{
int i = 0, j = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
Console.Write( arr[i, j] + " " );
Console.WriteLine();
}
}
static void find( int [,]arr)
{
arr[0, 0] = (arr[1, 2] + arr[2, 1]) / 2;
arr[2, 2] = (arr[0, 1] + arr[1, 0]) / 2;
arr[1, 1] = (arr[0, 0] + arr[1, 1]) / 2;
Console.Write( "Matrix with diagonals:\n" );
print(arr);
}
public static void Main()
{
int [,]arr = { { 0, 54, 48 },
{ 36, 0, 78 },
{ 66, 60, 0 } };
Console.Write( "Matrix initially:\n" );
print(arr);
find(arr);
}
}
|
PHP
<?php
function printt( $arr )
{
$i = 0;
$j = 0;
for ( $i = 0; $i < 3; $i ++)
{
for ( $j = 0; $j < 3; $j ++)
echo $arr [ $i ][ $j ], " " ;
echo "\n" ;
}
}
function find( $arr )
{
$arr [0][0] = ( $arr [1][2] + $arr [2][1]) / 2;
$arr [2][2] = ( $arr [0][1] + $arr [1][0]) / 2;
$arr [1][1] = ( $arr [0][0] + $arr [1][1]) / 2;
echo "Matrix with diagonals:\n" ;
printt( $arr );
}
$arr = array ( array ( 0, 54, 48 ),
array ( 36, 0, 78 ),
array ( 66, 60, 0 ));
echo "Matrix initially:\n" ;
printt( $arr );
find( $arr );
#This Code is contributed by ajit..
?>
|
Javascript
<script>
function print(arr)
{
let i = 0, j = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
document.write( parseInt(arr[i][j]) + " " );
document.write( "<br>" );
}
}
function find(arr)
{
arr[0][0] = (arr[1][2] + arr[2][1]) / 2;
arr[2][2] = (arr[0][1] + arr[1][0]) / 2;
arr[1][1] = (arr[0][0] + arr[1][1]) / 2;
document.write( "Matrix with diagonals:<br>" );
print(arr);
}
let arr = [[ 0, 54, 48 ],
[36, 0, 78 ],
[ 66, 60, 0 ]];
document.write( "Matrix initially:<br>" );
print(arr);
find(arr);
</script>
|
Output
Matrix initially:
0 54 48
36 0 78
66 60 0
Matrix with diagonals:
69 54 48
36 34 78
66 60 45
Complexity Analysis:
- Time Complexity: O(1), the code will run in a constant time.
- Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
08 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...