#C8953. Magic Square Rearrangement
Magic Square Rearrangement
Magic Square Rearrangement
You are given an integer n and a list of n2 integers. Your task is to check whether these numbers can be rearranged into an n × n magic square. A magic square is a square matrix in which the sum of every row, every column, and both main diagonals are equal. In a conventional magic square made of consecutive numbers from 1 to n2, this constant (also called the magic constant) is given by the formula: \( M = \frac{n(n^2+1)}{2} \).
If it is possible to form a magic square from the given numbers, print the square. Otherwise, print "No magic square can be formed".
Note: The input is provided from standard input (stdin) and the output should be printed to standard output (stdout). The numbers in the magic square must be printed such that each row is on a new line and the numbers in the row are separated by a space.
inputFormat
The first line of input contains a single integer n (the size of the square). The second line contains n2 space-separated integers.
outputFormat
If a magic square can be formed by rearranging the input array, output the magic square with each row printed on a new line and each number separated by a single space. Otherwise, output the string "No magic square can be formed".
## sample3
8 1 6 3 5 7 4 9 2
8 1 6
3 5 7
4 9 2
</p>