Check given matrix is magic square or not
Last Updated :
04 Aug, 2022
Given a matrix, check whether it’s Magic Square or not. A Magic Square is a n x n matrix of the distinct elements from 1 to n2 where the sum of any row, column, or diagonal is always equal to the same number.
Examples:
Input : n = 3
2 7 6
9 5 1
4 3 8
Output : Magic matrix
Explanation:In matrix sum of each
row and each column and diagonals sum is
same = 15.
Input : n = 3
1 2 2
2 2 1
2 1 2
Output : Not a Magic Matrix
Explanation:In matrix sum of each
row and each column and diagonals sum is
not same.
- Find the sum of prime diagonal and secondary diagonal.
- Calculate the sum of each row and column.
- If the prime diagonal and secondary diagonal sums are equal to every row’s sum and every column’s sum, then it is the magic matrix.
Implementation:
C++
#include <bits/stdc++.h>
# define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
using namespace std;
bool isMagicSquare( int mat[][3])
{
int n = my_sizeof(mat)/my_sizeof(mat[0]);
int i=0,j=0;
int sumd1 = 0, sumd2=0;
for (i = 0; i < n; i++)
{
sumd1 += mat[i][i];
sumd2 += mat[i][n-1-i];
}
if (sumd1!=sumd2)
return false ;
for (i = 0; i < n; i++) {
int rowSum = 0, colSum = 0;
for (j = 0; j < n; j++)
{
rowSum += mat[i][j];
colSum += mat[j][i];
}
if (rowSum != colSum || colSum != sumd1)
return false ;
}
return true ;
}
int main()
{
int mat[3][3] = {{ 2, 7, 6 },
{ 9, 5, 1 },
{ 4, 3, 8 }};
if (isMagicSquare(mat))
cout << "Magic Square" ;
else
cout << "Not a magic Square" ;
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
# define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
bool isMagicSquare( int mat[][3])
{
int n = my_sizeof(mat)/my_sizeof(mat[0]);
int i = 0, j = 0;
int sumd1 = 0, sumd2=0;
for (i = 0; i < n; i++)
{
sumd1 += mat[i][i];
sumd2 += mat[i][n-1-i];
}
if (sumd1!=sumd2)
return false ;
for (i = 0; i < n; i++) {
int rowSum = 0, colSum = 0;
for (j = 0; j < n; j++)
{
rowSum += mat[i][j];
colSum += mat[j][i];
}
if (rowSum != colSum || colSum != sumd1)
return false ;
}
return true ;
}
int main()
{
int mat[3][3] = {{ 2, 7, 6 },
{ 9, 5, 1 },
{ 4, 3, 8 }};
if (isMagicSquare(mat))
printf ( "Magic Square" );
else
printf ( "Not a magic Square" );
return 0;
}
|
Java
import java.io.*;
class GFG {
static int N = 3 ;
static boolean isMagicSquare( int mat[][])
{
int sumd1 = 0 ,sumd2= 0 ;
for ( int i = 0 ; i < N; i++)
{
sumd1 += mat[i][i];
sumd2 += mat[i][N- 1 -i];
}
if (sumd1!=sumd2)
return false ;
for ( int i = 0 ; i < N; i++) {
int rowSum = 0 , colSum = 0 ;
for ( int j = 0 ; j < N; j++)
{
rowSum += mat[i][j];
colSum += mat[j][i];
}
if (rowSum != colSum || colSum != sumd1)
return false ;
}
return true ;
}
public static void main(String[] args)
{
int mat[][] = {{ 2 , 7 , 6 },
{ 9 , 5 , 1 },
{ 4 , 3 , 8 }};
if (isMagicSquare(mat))
System.out.println( "Magic Square" );
else
System.out.println( "Not a magic" +
" Square" );
}
}
|
Python3
def isMagicSquare( mat) :
n = len (mat)
sumd1 = 0
sumd2 = 0
for i in range (n):
sumd1 + = mat[i][i]
sumd2 + = mat[i][n - i - 1 ]
if not (sumd1 = = sumd2):
return False
for i in range (n):
sumr = 0
sumc = 0
for j in range (n):
sumr + = mat[i][j]
sumc + = mat[j][i]
if not (sumr = = sumc = = sumd1):
return False
return True
mat = [ [ 2 , 7 , 6 ],
[ 9 , 5 , 1 ],
[ 4 , 3 , 8 ] ]
if (isMagicSquare(mat)) :
print ( "Magic Square" )
else :
print ( "Not a magic Square" )
|
C#
using System;
class GFG
{
static int N = 3;
static bool isMagicSquare( int [,] mat)
{
int sumd1 = 0, sumd2 = 0;
for ( int i = 0; i < N; i++)
{
sumd1 = sumd1 + mat[i, i];
sumd2 = sumd2 + mat[i, N-1-i];
}
if (sumd1!=sumd2)
return false ;
for ( int i = 0; i < N; i++) {
int rowSum = 0, colSum = 0;
for ( int j = 0; j < N; j++)
{
rowSum += mat[i, j];
colSum += mat[j,i];
}
if (rowSum != colSum || colSum != sumd1)
return false ;
}
return true ;
}
public static void Main()
{
int [,] mat = new int [,] {{ 2, 7, 6 },
{ 9, 5, 1 },
{ 4, 3, 8 }};
if (isMagicSquare(mat))
Console.WriteLine( "Magic Square" );
else
Console.WriteLine( "Not a magic" +
" Square" );
}
}
|
PHP
<?php
function isMagicSquare( $mat )
{
$sumd1 = 0; $sumd2 = 0;
$N = count ( $mat );
for ( $i = 0; $i < $N ; $i ++)
{
$sumd1 = $sumd1 + $mat [ $i ][ $i ];
$sumd2 = $sumd2 + $mat [ $i ][ $N - $i -1];
}
if ( $sumd1 != $sumd2 )
return false;
for ( $i = 0; $i < $N ; $i ++)
{
$rowSum = 0; $colSum = 0;
for ( $j = 0; $j < $N ; $j ++)
{
$rowSum += $mat [ $i ][ $j ];
$colSum += $mat [ $j ][ $i ];
}
if ( $rowSum != $colSum || $colSum != $sumd1 )
return false;
}
return true;
}
{
$mat = array ( array (2, 7, 6),
array (9, 5, 1),
array (4, 3, 8));
if (isMagicSquare( $mat ))
echo "Magic Square" ;
else
echo "Not a magic Square" ;
return 0;
}
?>
|
Javascript
<script>
function isMagicSquare(mat)
{
var N = mat.length
var sumd1 = 0,sumd2=0;
for ( var i = 0; i < N; i++)
{
sumd1 = sumd1 + mat[i][i];
sumd2 = sumd2 + mat[i][N-1-i];
}
if (sumd1!=sumd2)
return false ;
for ( var i = 0; i < N; i++) {
var colSum = 0;
var rowSum = 0;
for ( var j = 0; j < N; j++)
{
rowSum += mat[i][j];
colSum += mat[j][i];
}
if (rowSum != colSum || colSum != sumd1)
return false ;
}
return true ;
}
var mat = [[ 2, 7, 6 ],
[ 9, 5, 1 ],
[ 4, 3, 8 ]];
if (isMagicSquare(mat))
document.write( "Magic Square" );
else
document.write( "Not a magic Square" );
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...