Magic Squares
Totos needs to finally complete his term paper in computer science. The topic he got was magic squares. He's reading on Wikipedia:
"A magic square is an arrangement of numbers in an array of equal sets of rows and columns, where the arithmetic operation between numbers in the same row or column or diagonal of the square always returns the same result. This common result is called the magic constant of the magic square. The most common arithmetic operation in magic squares is addition between numbers, and there are other versions... "
Example of a 3x3 magic square where the sum of the numbers in each row, column and diagonal equals .
The algorithm for its construction is giving him a headache. It seems easier for him to use a random number generator and select the ones that form a magic square.
We want to write a program that reads from the input STDIN
a positive integer , followed by unique positive integers , separated by a space character (' '
) or a line break ('\n'
), and checks whether the given square is a magic square.
The program will print STDOUT
output "NO " if the input square does not correspond to a magic square or will print "YES " on the first line of output followed by a number on the next line if the given square corresponds to a magic square.
- () represents the size of the side of the square.
- (, ) denote each unique number of the square.
- denotes the sum of a row, column or diagonal of the magic square.
Attention! Each input or output line should end with the line change character '\n'
and the words "YES" and "NO" are written in roman characters.
Examples
1st
STDIN
3
2 7 6
9 5 1
4 3 8
STDOUT
YES
15
2nd
STDIN
3
2 7 4
1 8 9
6 3 5
STDOUT
NO
Comments