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

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.

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

**Output:**

2

## Recommended Posts:

- Minimum cost to convert 3 X 3 matrix into magic square
- Check given matrix is magic square or not
- Maximum and Minimum in a square matrix.
- Minimum operations of given type to make all elements of a matrix equal
- Minimum flip required to make Binary Matrix symmetric
- Minimum operations required to make each row and column of matrix equals
- Magic Square
- Magic Square | Even Order
- Fill missing entries of a magic square
- Check if it is possible to make the given matrix increasing matrix or not
- How to access elements of a Square Matrix
- Maximum size square sub-matrix with all 1s
- Inplace rotate square matrix by 90 degrees | Set 1
- Print maximum sum square sub-matrix of given size
- Sum of both diagonals of a spiral odd-order square matrix

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.