#K71632. Magical Square Checker
Magical Square Checker
Magical Square Checker
You are given a 3x3 grid of integers. Your task is to determine if the grid forms a magical square. A magical square is defined as a square in which the sum of the numbers in each row, each column, and both main diagonals are all equal. More formally, let the grid be represented as:
\( grid = \begin{bmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{bmatrix} \)
It is a magical square if:
- \( a_{11} + a_{12} + a_{13} = a_{21} + a_{22} + a_{23} = a_{31} + a_{32} + a_{33} \)
- \( a_{11} + a_{21} + a_{31} = a_{12} + a_{22} + a_{32} = a_{13} + a_{23} + a_{33} \)
- \( a_{11} + a_{22} + a_{33} = a_{13} + a_{22} + a_{31} \)
If the above conditions hold, output YES
; otherwise, output NO
.
inputFormat
The input consists of 3 lines, each line containing 3 space-separated integers representing a row of the grid.
For example:
8 1 6 3 5 7 4 9 2
outputFormat
Output a single line with YES
if the given grid is a magical square, or NO
otherwise.
8 1 6
3 5 7
4 9 2
YES