#K40232. Magic Square in Subarray

    ID: 26597 Type: Default 1000ms 256MiB

Magic Square in Subarray

Magic Square in Subarray

Problem Statement:

Given an array of integers, determine whether any contiguous subarray of exactly 9 elements, when arranged into a 3×3 matrix in row-major order, forms a magic square. A magic square is defined as a 3×3 matrix in which the sum of the elements in each row, each column, and both the main diagonals are equal. In mathematical terms, for a matrix A, it must satisfy:

$$\sum_{j=1}^{3} A_{ij} = S \quad (\forall; i = 1,2,3)$$
$$\sum_{i=1}^{3} A_{ij} = S \quad (\forall; j = 1,2,3)$$
$$A_{11}+A_{22}+A_{33} = S$$
$$A_{13}+A_{22}+A_{31} = S$$

If such a subarray exists, print YES, otherwise print NO.

Note: If the total number of integers is less than 9, the answer must be NO.

inputFormat

The input is read from standard input. The first line contains an integer n (n ≥ 1) denoting the number of integers. The second line contains n space-separated integers.

outputFormat

Output a single line to standard output: YES if there exists a contiguous subarray forming a magic square, otherwise NO.## sample

4
2 7 6 9
NO