Step by step explanation of Forming a magic square problem on HackerRank with code at the end HackerRank profile: www.hackerrank... LinkedIn: / anurag-patel-101
There are only 8 unique magic squares of 3x3 form, and we are finding the absolute difference between these(all 8) and our given matrix and finally printing the minimum value out of it. Hope this helps 👍
I don't understand explanation behind statement that odd element must be in the center. How he made that conclusion based on fact that "even+even=even, odd+even=odd, odd+odd=even" - how does it help to understand that in the middle must be odd number?
we want sum of all rows cols and diagonals t be odd tht is 15, and odd= even + odd+even or odd=odd+odd+even . if we use up one of four even numbers in the center we wont have sufficient even number combinations for rest rows cols and diags to make their sum odd
This is not a long explanation, but you'll have to "read more" because i use line breaks for clarity Let's look at the four possibilities of sums if the center value is even. I'll put the center value in CAPS. Remember that the sum MUST be 15 (odd) even + EVEN + even = even (ILLEGAL!) even + EVEN + odd = odd (Allowed) odd + EVEN + even = odd (Allowed) odd + EVEN + odd = even (ILLEGAL!) Therefore, the only way to make a valid magic square with an EVEN number in the center, is if the other two values are OPPOSITE (one even and one odd) This means that you have the same quantity of even and odd numbers. But if you use an even number in the center, you are left with 3 remaining even numbers (2 4 6 8 but you used one in the center already) and you are left with 5 odd numbers (1 3 5 7 9). Therefore, you CAN'T match up one even with one odd. You will eventually be forced to do odd + EVEN + odd (which is ILLEGAL!). Therefore, you can guarantee that there is an odd number in the center, as an even number would be impossible.
If we consider 1 as center number - | - | - - | 1 | - - | - | - The next smaller number available is 2. If u place the number 2, anywhere along the empty space, the counterpart will have to be 13. This is not possible because the largest number is 9. eg: 2 | - | - - | 1 | - - | - | 13