#K42612. Special Matrix Formation
Special Matrix Formation
Special Matrix Formation
Given an integer \( N \) and a sequence of \( N^2 \) integers, determine whether it is possible to rearrange these numbers into an \( N \times N \) matrix such that the sum of the elements in every row and every column is equal.
Let \( S \) be the total sum of all the elements. In order for such an arrangement to be possible, \( S \) must be divisible by \( N \). The desired row (and column) sum is then given by \( T = S/N \). Your task is to check if there exists any permutation of the input sequence that yields a matrix where every row and every column sums up to \( T \). Note that a \( 1 \times 1 \) matrix always satisfies this property.
inputFormat
The first line contains an integer \( N \) representing the dimension of the matrix. The second line contains \( N^2 \) space-separated integers.
outputFormat
Output Yes
if it is possible to rearrange the elements to form a special matrix where each row and each column sums to \( T = S/N \); otherwise, output No
.
3
2 7 6 9 5 1 4 3 8
Yes