#C11240. Magic Square Formation
Magic Square Formation
Magic Square Formation
Given a positive integer (N) and a list of (N^2) integers, determine whether it is possible to arrange the integers into an (N \times N) grid such that every row, every column, and both main diagonals have the same sum. The required magic sum for each row, column, and diagonal is given by
[
\frac{N(N^2+1)}{2}
]
Print YES
if such an arrangement exists, otherwise print NO
.
inputFormat
The input is provided via standard input (stdin) in two lines. The first line contains a single integer (N) representing the size of the square. The second line contains (N^2) space-separated integers.
outputFormat
Output a single line to standard output (stdout): YES
if the numbers can be rearranged to form a magic square, otherwise NO
.## sample
3
8 1 6 3 5 7 4 9 2
YES