Check Given Matrix is magic square or not

Here is the structured documentation for the GeeksforGeeks Problem: Magic Square following your preferred format:


Magic Square – GeeksforGeeks


Problem: Magic Square

Problem Statement

Given an n × n matrix mat[][], check whether the matrix is a Magic Square.

A Magic Square is a matrix that satisfies two conditions:

  1. It contains distinct elements from 1 to .

  2. The sum of every row, every column, and both main diagonals is the same.

Return "Magic Square" if the matrix satisfies both conditions, otherwise return "Not a Magic Square".


Examples

Example 1
Input: mat = [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
Output: "Magic Square"
Explanation:

Example 2
Input: mat = [[1, 2], [3, 4]]
Output: "Not a Magic Square"
Explanation:

Example 3
Input: mat = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
Output: "Not a Magic Square"
Explanation:


Constraints


Intuition

A matrix can only be a magic square if:

First, compute the expected sum:

Sum=n2(n2+1)2

Then verify the sum of each row, column, and both diagonals. Also, validate the presence of all distinct numbers from 1 to .


Approach

  1. Calculate compVal = target sum for all rows/cols/diagonals.

  2. Compute and compare the sum of:

    • Primary diagonal

    • Secondary diagonal

    • Each row

    • Each column

  3. Return "Magic Square" if all checks pass, else return "Not a Magic Square".

Note: The given solution misses the check for distinct numbers from 1 to , which is a required condition for a magic square.


Code (Java)

class Solution {
    public static String magicSquare(int mat[][]) {
        String isA = "Magic Square";
        String notA = "Not a Magic Square";
        int n = mat.length;
        int totalNum = n * n;
        int compVal = ((totalNum * (totalNum + 1)) / 2) / n;

        // Check diagonals
        int fDSum = 0, rDSum = 0;
        for (int i = 0; i < n; i++) {
            fDSum += mat[i][i];
            rDSum += mat[i][n - i - 1];
        }
        if (fDSum != compVal || rDSum != compVal) return notA;

        // Check rows
        for (int i = 0; i < n; i++) {
            int row = 0;
            for (int j = 0; j < n; j++) {
                row += mat[i][j];
            }
            if (row != compVal) return notA;
        }

        // Check columns (fixing original mistake)
        for (int j = 0; j < n; j++) {
            int col = 0;
            for (int i = 0; i < n; i++) {
                col += mat[i][j];
            }
            if (col != compVal) return notA;
        }

        // Optional: Check distinctness from 1 to n^2
        boolean[] seen = new boolean[totalNum + 1];
        for (int[] row : mat) {
            for (int val : row) {
                if (val < 1 || val > totalNum || seen[val]) return notA;
                seen[val] = true;
            }
        }

        return isA;
    }
}

Time and Space Complexity


Dry Run

Input: mat = [[2,7,6],[9,5,1],[4,3,8]], n = 3


Conclusion

This problem combines matrix traversal with basic arithmetic and validation logic. A complete solution must not only check the sums of rows/columns/diagonals but also verify that all numbers from 1 to are distinct and present once.