#C8253. Magic Square Checker

    ID: 52215 Type: Default 1000ms 256MiB

Magic Square Checker

Magic Square Checker

You are given a 3x3 grid of integers. Your task is to determine whether the grid forms a magic square. A magic square is a grid where the sum of the numbers in each row, each column, and both the main diagonals are equal. In mathematical terms, if we denote the grid as \(a_{ij}\) (with \(i,j \in \{1,2,3\}\)), the grid is magic if:

[ \sum_{j=1}^{3} a_{1j} = \sum_{j=1}^{3} a_{2j} = \sum_{j=1}^{3} a_{3j} = \sum_{i=1}^{3} a_{i1} = \sum_{i=1}^{3} a_{i2} = \sum_{i=1}^{3} a_{i3} = a_{11}+a_{22}+a_{33} = a_{13}+a_{22}+a_{31} ]

If the grid does not have exactly 3 rows or any row does not have exactly 3 integers, consider it invalid and output NO. Otherwise, if the grid is a magic square, output YES; if not, output NO.

inputFormat

The input is read from standard input (stdin) and consists of several lines. Each line contains space-separated integers representing a row of the grid. The grid might have invalid dimensions; if it does not consist of exactly 3 rows with 3 integers each, it must be treated as invalid.

Example Input:

2 7 6
9 5 1
4 3 8

outputFormat

Print to standard output (stdout) a single line with either YES if the grid forms a magic square, or NO otherwise.

Example Output:

YES
## sample
2 7 6
9 5 1
4 3 8
YES