public static boolean isMagicSquare(int[][] arr) {
        final int n = arr.length;
        final int nSquare = n * n;
        final int M = (n * n * (n * n + 1) / 2) / n;
        int sumOfRow = 0, sumOfColoumns = 0, sumOfPrimaryDiagonal = 0, sumOfSecondaryDiagonal = 0;
        boolean[] flag = new boolean[n * n];
        sumOfRows = new int[n];
        sumOfCols = new int[n];
        for (int row = 0; row < n; row++) {
            sumOfRow = 0;
            sumOfColoumns = 0;
            for (int col = 0; col < n; col++) {
                if (arr[row][col] < 1 || arr[row][col] > nSquare) {
                    return false;
                }

                if (flag[arr[row][col] - 1] == true) {
                    return false;
                }

                flag[arr[row][col] - 1] = true;
                sumOfRow += arr[row][col];
                sumOfColoumns += arr[col][row];
            }
            sumOfPrimaryDiagonal += arr[row][row];
            sumOfSecondaryDiagonal += arr[row][n - row - 1];
            sumOfRows[row] = sumOfRow;
            sumOfCols[row] = sumOfColoumns;
            if (sumOfRow != M || sumOfColoumns != M) {
                return false;
            }
        }
        sumOfPrimaryDiag = sumOfPrimaryDiagonal;
        sumOfSecDiag = sumOfSecondaryDiagonal;
        if (sumOfPrimaryDiagonal != M || sumOfSecondaryDiagonal != M) {
            return false;
        }
        return true;
    }