#K84757. Magic Square Checker
Magic Square Checker
Magic Square Checker
You are given an n × n grid filled with integers. A magic square is a grid where the numbers from 1 to n2 each appear exactly once and the sum of the numbers in each row, each column, and both main diagonals is equal. This common sum is known as the magic constant and can be computed using the formula:
\( M = \frac{n(n^2+1)}{2} \)
Your task is to determine whether a given grid is a magic square. If it is, print YES
; otherwise, print NO
.
Note: The input grid will always be provided in such a way that it contains n rows and each row contains n space-separated integers.
inputFormat
The first line contains an integer n
representing the size of the grid. The following n
lines each contain n
space-separated integers forming the grid.
For example:
3 8 1 6 3 5 7 4 9 2
outputFormat
Output a single line with the string YES
if the grid is a magic square, or NO
otherwise.
3
8 1 6
3 5 7
4 9 2
YES