public static boolean
allNumsInArr(int [][]arr) {
int x = arr.length*arr.length;
int [] tmp = new int[x];
for (int row =0;row<arr.length;row++) {
for(int col = 0; col<arr[0].length;col++) {
int num = arr[row][col];
if (num < 1
|| num > x) {
return false;
}
tmp[num-1]++;
}
}
for(int i=0;i<tmp.length;i++) {
if (tmp[i] != 1)
{
return false;
}
}
return true;
}
public static int
calcRowInArr(int [][] arr, int row) {
int sum = 0;
for (int col=0;col<arr[0].length;col++) {
sum += arr[row][col];
}
return sum;
}
public static int
calcColInArr(int [][] arr, int col) {
int sum = 0;
for (int row=0;row<arr.length;row++) {
sum += arr[row][col];
}
return sum;
}
public static boolean
checkAllRows(int [][] arr,int sum) {
for (int row = 0; row<arr.length; row++) {
if (calcRowInArr(arr,row) != sum) {
return false;
}
}
return true;
}
public static boolean
checkAllCols(int [][] arr,int sum) {
for (int col = 0; col<arr[0].length; col++) {
if (calcColInArr(arr,col) != sum) {
return false;
}
}
return true;
}
public static boolean
checkDiagonals(int [][] arr,int sum) {
int s1=0,s2=0;
for (int i=0;i<arr.length;i++) {
s1 += arr[i][i];
s2 += arr[i][arr.length -1-i];
}
return (sum == s1
&& sum == s2);
}
public static boolean
checkMagicSQ(int[][] arr) {
if (allNumsInArr(arr)) { // בודקת שכל המספרים במערך
int sum = calcRowInArr(arr,0); // מחשבת סכום של שורה
if (checkAllRows(arr,sum)
&& checkAllCols(arr,sum)
&& checkDiagonals(arr,sum)) {
return true;
}
}
return false;
}