Open In App

How to Check if the Matrix is a Magic Square in JavaScript?

Last Updated : 16 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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)); 

Output
true

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)); 

Output
true

Time complexity: O(n^2)

Space complexity: O(1)


Next Article

Similar Reads