Determine whether a small square grid is an odd-sized magic square: all rows, columns, and diagonals have the same sum.
Problem
You are given an square grid of integers. Your task is to determine whether the grid is a magic square.
A square grid is considered a magic square if:
- the sums of all rows are equal,
- the sums of all columns are equal,
- the sums of the two main diagonals are equal,
- and all of these sums are the same value.
For this problem, the grid is small enough that direct checking of all required sums is sufficient.
Goal
Return whether the provided square grid satisfies the magic square condition.
Input Format
- The first line contains an integer .
- The next lines contain integers each, describing the grid.
Output Format
- Print
YESif the grid is a magic square. - Otherwise, print
NO.
Constraints
- The grid is square with rows and columns.
- Integer values fit in standard 32-bit signed range.
- A direct check is sufficient.
Example 1
Input
3 8 1 6 3 5 7 4 9 2
Output
YES
Explanation
Every row, column, and both diagonals sum to 15.
Example 2
Input
3 1 2 3 4 5 6 7 8 9
Output
NO
Explanation
The row sums are all 6, 15, and 24, so the grid is not a magic square.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.