Minimum changes needed to make a 3*3 matrix magic square

Given a 3*3 matrix, find the minimum number of changes that need to be made to it in order to turn it into a magic square. A magic square is a square matrix whose sum of all the rows are the same, the sum of all the columns are the same and the sum of both the diagonals are the same.

Examples:

```Input :  8 5 6
3 8 7
4 9 2
Output : 2
5 in row 1 col 2 should be changed to 1
8 in row 2 col 2 should be changed to 5
Total 2 changes are required.

Input : 8 9 4
5 9 3
6 1 8
Output : 3
8 in row 1 col 1 should be changed to 2
5 in row 2 col 1 should be changed to 7
9 in row 2 col 2 should be changed to 5
Total 3 changes are required.
```

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

There are 8 possible magic squares of 3*3 dimensions, which are

8 1 6
3 5 7
4 9 2

6 1 8
7 5 3
2 9 4

4 9 2
3 5 7
8 1 6

2 9 4
7 5 3
6 1 8

8 3 4
1 5 9
6 7 2

4 3 8
9 5 1
2 7 6

6 7 2
1 5 9
8 3 4

2 7 6
9 5 1
4 3 8

We create a 3D array to store these 8 matrices. Now we check the input matrix for each of these 8 matrices and find out the one which can be obtained with the least number of changes.

Java

 `class` `GfG { ` ` `  `    ``// this program takes in two 2D arrays as ` `    ``// input and compares them to find out the ` `    ``// minimum number of changes that needs to ` `    ``// be made to convert arr to ms. ` `    ``public` `static` `int` `findMinimumFromMS(``int``[][] arr, ` `                                        ``int``[][] ms) ` `    ``{ ` `        ``int` `count = ``0``; ` `        ``for` `(``int` `i = ``0``; i < ``3``; i++) { ` `            ``for` `(``int` `j = ``0``; j < ``3``; j++) { ` `                ``if` `(arr[i][j] != ms[i][j]) ` `                    ``count++; ` `            ``} ` `        ``} ` `        ``return` `count; ` `    ``} ` ` `  `    ``public` `static` `int` `findMinimum(``int``[][] arr) ` `    ``{ ` `        ``int``[][][] ms = { ` `            ``{ { ``8``, ``1``, ``6` `}, { ``3``, ``5``, ``7` `}, { ``4``, ``9``, ``2` `} }, ` `            ``{ { ``6``, ``1``, ``8` `}, { ``7``, ``5``, ``3` `}, { ``2``, ``9``, ``4` `} }, ` `            ``{ { ``4``, ``9``, ``2` `}, { ``3``, ``5``, ``7` `}, { ``8``, ``1``, ``6` `} }, ` `            ``{ { ``2``, ``9``, ``4` `}, { ``7``, ``5``, ``3` `}, { ``6``, ``1``, ``8` `} }, ` `            ``{ { ``8``, ``3``, ``4` `}, { ``1``, ``5``, ``9` `}, { ``6``, ``7``, ``2` `} }, ` `            ``{ { ``4``, ``3``, ``8` `}, { ``9``, ``5``, ``1` `}, { ``2``, ``7``, ``6` `} }, ` `            ``{ { ``6``, ``7``, ``2` `}, { ``1``, ``5``, ``9` `}, { ``8``, ``3``, ``4` `} }, ` `            ``{ { ``2``, ``7``, ``6` `}, { ``9``, ``5``, ``1` `}, { ``4``, ``3``, ``8` `} }, ` `        ``}; ` ` `  `        ``// If all the elements need to be changed, ` `        ``// there would be 9 changes, so we take the ` `        ``// max as 9 ` `        ``int` `min = ``9``; ` `        ``for` `(``int` `i = ``0``; i < ``8``; i++) { ` `            ``int` `x = findMinimumFromMS(arr, ms[i]); ` `            ``if` `(x < min) ` `                ``min = x; ` `        ``} ` `        ``return` `min; ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int``[][] arr = { { ``8``, ``5``, ``6` `}, { ``3``, ``8``, ``7` `},  ` `                                     ``{ ``4``, ``9``, ``2` `} }; ` `        ``System.out.println(findMinimum(arr)); ` `    ``} ` `} `

Python3

 `# This program takes in two 2D arrays as  ` `# input and compares them to find out the  ` `# minimum number of changes that needs to  ` `# be made to convert arr to ms.  ` `def` `findMinimumFromMS(arr, ms): ` `     `  `    ``count ``=` `0`  `    ``for` `i ``in` `range``(``0``, ``3``):  ` `        ``for` `j ``in` `range``(``0``, ``3``):  ` `            ``if` `arr[i][j] !``=` `ms[i][j]:  ` `                ``count ``+``=` `1` `     `  `    ``return` `count  ` ` `  `def` `findMinimum(arr):  ` `  `  `    ``ms ``=` `[  ` `            ``[ [ ``8``, ``1``, ``6` `], [ ``3``, ``5``, ``7` `], [ ``4``, ``9``, ``2` `] ],  ` `            ``[ [ ``6``, ``1``, ``8` `], [ ``7``, ``5``, ``3` `], [ ``2``, ``9``, ``4` `] ],  ` `            ``[ [ ``4``, ``9``, ``2` `], [ ``3``, ``5``, ``7` `], [ ``8``, ``1``, ``6` `] ],  ` `            ``[ [ ``2``, ``9``, ``4` `], [ ``7``, ``5``, ``3` `], [ ``6``, ``1``, ``8` `] ],  ` `            ``[ [ ``8``, ``3``, ``4` `], [ ``1``, ``5``, ``9` `], [ ``6``, ``7``, ``2` `] ],  ` `            ``[ [ ``4``, ``3``, ``8` `], [ ``9``, ``5``, ``1` `], [ ``2``, ``7``, ``6` `] ],  ` `            ``[ [ ``6``, ``7``, ``2` `], [ ``1``, ``5``, ``9` `], [ ``8``, ``3``, ``4` `] ],  ` `            ``[ [ ``2``, ``7``, ``6` `], [ ``9``, ``5``, ``1` `], [ ``4``, ``3``, ``8` `] ],  ` `        ``]  ` ` `  `    ``# If all the elements need to be changed, there  ` `    ``# would be 9 changes, so we take the max as 9  ` `    ``Min` `=` `9`  `    ``for` `i ``in` `range``(``0``, ``8``):  ` `        ``x ``=` `findMinimumFromMS(arr, ms[i])  ` `        ``if` `x < ``Min``:  ` `            ``Min` `=` `x ` `     `  `    ``return` `Min`  ` `  `if` `__name__ ``=``=` `"__main__"``: ` `  `  `    ``arr ``=` `[ [ ``8``, ``5``, ``6` `], [ ``3``, ``8``, ``7` `], [ ``4``, ``9``, ``2` `] ]  ` `    ``print``(findMinimum(arr))  ` `  `  `# This code is contributed by Rituraj Jain `

Output:

```2
```

My Personal Notes arrow_drop_up

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : rituraj_jain

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.