We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Forming a Magic Square
- Discussions
Forming a Magic Square
Forming a Magic Square
Sort by
recency
|
39 Discussions
|
Please Login in order to post a comment
This is a way better problem than misery nim. Yes, you can know before hand all magic squares (as you can you know the xor solutions), but, here, you can even deduce all the magic squares or build them all during the interview, which is probably what is expected. However, building them somewhat efficiently instead of creating all 9! and then filtering
Python best solution
If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions
Idk why all the python answers assume you'd have access to a list of all magic squares when doing this problem.I guess to game the hackerrank rankings? Not very helpful for people actually trying to learn. Anyways, Here's how I would solve this on an actual test, including code to find all magic squares:
I have no clue how you supossed to figure this out on your own without serious matrix theory knowladge, and the knowladge of this silly trivia magic array, here is the info: There are fixed 9 numbers in 3x3 magic array, there is no other choice. Thje example gives this. The only different arrays can either 90 degree rotations of this magic array. Or a reflection on the middle, then this can also be rotated 90 degrees 3 times to get different versions. Good luck figuring this out on an interview.. ???
int matrix_diff(vector>& A, vector>& B){ int diff=0; for(int i=0;i
void transpose(vector>& A){ for(int i=0; i
void reflect(vector>& A){ for(int i=0; i
int formingMagicSquare(vector> s) { std::vector> magic = {{8,3,4},{1,5,9},{6,7,2}}; //there is only one magic array, and its 8 versions, by 3* 90degree rotation, then 1 reflection and 3rotations again //make another version, then compare diff to s, and record minimum; int min_global_diff=matrix_diff(s, magic);
}
Python 3:
Solution in CPP, building all magic squares: