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.

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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum cost to convert 3 X 3 matrix into magic square
- Minimum changes required to make each path in a matrix palindrome
- Check given matrix is magic square or not
- Magic Square
- Magic Square | Even Order
- Fill missing entries of a magic square
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Check if it is possible to make the given matrix increasing matrix or not
- Maximum and Minimum in a square matrix.
- Minimum flip required to make Binary Matrix symmetric
- Minimum operations required to make each row and column of matrix equals
- Minimum operations of given type to make all elements of a matrix equal
- Count Magic squares in a grid
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Maximum size square sub-matrix with all 1s
- Given an n x n square matrix, find sum of all sub-squares of size k x k
- Print maximum sum square sub-matrix of given size

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.