S. Freeman

Greenhorn

Posts: 12

posted 3 years ago

Magic square - n × n(4 x 4 in this case) matrix that is filled with the numbers 1, 2, 3, . . ., n2

Conditions: 1.Are the numbers 1, 2, ..., 16 the input.

2. And is the sums of the rows, columns, and diagonals equal to each other?

Question/request for assistance one: i wonder if there is a more optimal way of comparing elements of two arrays. I predefined an array at line 67 expectedArr, and then removed elements from that array that fall into the right-input category. So basically i am wondering how can checking if one array contains all the elements of another arrays be done better, an easier way?

And at line 120. i first ran with return equalSumRowsColumns (arr, a++) && sumOfRow(arr[a]) == sumOfColumn(arr, a); which lead to the following error -> java.lang.StackOverflowError. But when i replaced it with a + 1 there is no error. I thought that maybe a++ 'instantly' changes the value of a so on next uses it is already updated when it shouldn't be, so i changed it to a + 1, but isnt a++ same as a + 1, confused...

Edit1: example of a magic square

Conditions: 1.Are the numbers 1, 2, ..., 16 the input.

2. And is the sums of the rows, columns, and diagonals equal to each other?

Question/request for assistance one: i wonder if there is a more optimal way of comparing elements of two arrays. I predefined an array at line 67 expectedArr, and then removed elements from that array that fall into the right-input category. So basically i am wondering how can checking if one array contains all the elements of another arrays be done better, an easier way?

And at line 120. i first ran with return equalSumRowsColumns (arr, a++) && sumOfRow(arr[a]) == sumOfColumn(arr, a); which lead to the following error -> java.lang.StackOverflowError. But when i replaced it with a + 1 there is no error. I thought that maybe a++ 'instantly' changes the value of a so on next uses it is already updated when it shouldn't be, so i changed it to a + 1, but isnt a++ same as a + 1, confused...

Edit1: example of a magic square

Stephan van Hulst

Saloon Keeper

Posts: 7993

143

posted 3 years ago

- 1

Hi S. Freeman,

Consider that you are iterating over

What you really need to do is check whether each element in

As for your second question, given

Consider that you are iterating over

`expectedArr`multiple times for every value of`arr`, and on top of that you're also copying almost the entire array for each element of`arr`. The answer is yes, there's a more efficient way.What you really need to do is check whether each element in

`arr`is unique, and none are smaller than 1 or greater than 16. An easy way of checking whether elements are unique, is by putting them all in a set and then seeing if the set is the same size as the original sample group.`HashMap<Integer>`

`HashSet<Integer>`would be very useful for this purpose. Then, as you're passing every element to the set, you only have to check whether they're within the specified range.As for your second question, given

`a=0`,`a++`does *not* equal 1. Look up "post-increment-operator".*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

Stephan van Hulst

Saloon Keeper

Posts: 7993

143