How to Check if the Matrix is a Magic Square in JavaScript?
Last Updated :
16 May, 2024
We are going to Check if the Matrix is a Magic Square in JavaScript. A matrix is said to be Magic Square if the sum of elements of any row, column or diagonal is equal to a specific number. Below is an example to understand the problem clearly.
Example:
Input:
[ [2, 7, 6] ,
[9, 5, 1] ,
[4, 3, 8] ] ;
Output: True
Explanation: The sum of elements of every row, column and diagonal is equal to a specific number.
Below are the approaches in JavaScript to Check if the matrix is a magic square or not:
Naive Approach
In this method, calculate the sum of the first row. Then, iterate through each row and column. If any row's or column's sum doesn't match the sum of the first row, return false. Next, calculate the sums of the main and secondary diagonals. If either doesn't match the sum of the first row, return false. If all checks pass, return true.
Example: To demonstrate checking if the matrix is a magic square in JavaScript using brute force approach
JavaScript
function magicSquare(matrix) {
const n = matrix.length;
let sum = 0;
for (let j = 0; j < n; j++) {
sum += matrix[0][j];
}
for (let i = 1; i < n; i++) {
let rowSum = 0;
for (let j = 0; j < n; j++) {
rowSum += matrix[i][j];
}
if (rowSum !== sum) {
return false;
}
}
for (let j = 0; j < n; j++) {
let columnSum = 0;
for (let i = 0; i < n; i++) {
columnSum += matrix[i][j];
}
if (columnSum !== sum) {
return false;
}
}
let diagonalSum = 0;
for (let i = 0; i < n; i++) {
diagonalSum += matrix[i][i];
}
if (diagonalSum !== sum) {
return false;
}
diagonalSum = 0;
for (let i = 0; i < n; i++) {
diagonalSum += matrix[i][n - i - 1];
}
if (diagonalSum !== sum) {
return false;
}
return true;
}
const matrix = [
[2, 7, 6],
[9, 5, 1],
[4, 3, 8]
];
console.log(magicSquare(matrix));
Time complexity: O(n^2)
Space complexity: O(1)
Using Mathematical Properties
To verify if a matrix is a magic square, use the formula n * (n^2 + 1) / 2 to calculate the expected sum for each row, column, and diagonal, where n is the matrix size. Iterate through rows and columns, summing elements and comparing with the expected sum. Additionally, compute the sums of the main and secondary diagonals. If any sum deviates from the expected value, return false; otherwise, return true if all sums align with the expected sum.
Example: To demonstrate checking if the matrix is a magic square in JavaScript using mathematical properties approach.
JavaScript
function magicSquare(matrix)
{
const n = matrix.length;
const expectedSum = n * (n * n + 1) / 2;
let rowSum = 0, colSum = 0, mainDiagSum = 0;
for (let i = 0; i < n; i++) {
rowSum = 0;
colSum = 0;
for (let j = 0; j < n; j++) {
rowSum += matrix[i][j];
colSum += matrix[j][i];
}
if (rowSum !== expectedSum || colSum !== expectedSum)
{
return false;
}
mainDiagSum += matrix[i][i];
}
let secondaryDiagSum = 0;
for (let i = 0; i < n; i++) {
secondaryDiagSum += matrix[i][n - i - 1];
}
if (secondaryDiagSum !== expectedSum || mainDiagSum !== expectedSum) {
return false;
}
return true;
}
const matrix = [
[2, 7, 6],
[9, 5, 1],
[4, 3, 8]
];
console.log(magicSquare(matrix));
Time complexity: O(n^2)
Space complexity: O(1)
Similar Reads
JavaScript Program to Find the Area of a Square In this article, we will see how to find the area of a square in JavaScript if the side of the square is given. A square is a geometric shape with four equal sides and four right angles. The area of a square is calculated by multiplying the length of one side by itself. Formula of the Area of Square
3 min read
Largest Square Formed in a Matrix using JavaScript Given a matrix, our task is to find the largest square formed in a matrix using JavaScript. The largest square is a square that contains the same number of rows and columns and no more squares can be possible to form in that matrix that contains more rows and columns than this square. The below meth
3 min read
JavaScript Program to check if matrix is lower triangular Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 0, 0, 0}, {1, 4, 0, 0}, {4, 6, 2, 0}, {0, 4, 7, 6}};Output : Matrix is in l
2 min read
Javascript Program to check if matrix is upper triangular Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero.Examples: Input : mat[4][4] = {{1, 3, 5, 3}, {0, 4, 6, 2}, {0, 0, 2, 5}, {0, 0, 0, 6}};Output : Matrix is in Up
2 min read
JavaScript Program to Check Whether a Number is Perfect Square The number that results from squaring another integer is called a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself. There are several ways available in JavaScript to check whether a number is a perfect square or not which are as follows: Tab
4 min read