Given a matrix A of size N such that it has only one 0, Find the number to be placed in place of the 0 such that sum of the numbers in every row, column and two diagonals become equal.
Note: Diagonals should be only of the form A[i][i] and A[i][N-i-1]. If there is a matrix of size 1, with an element 0, then print 1.
First line contains the test cases, T . Then T test cases follow. Each test case contains a single integer N which represents the number of rows and columns of the matrix. Then in the next line are N*N space separated of the matrix A.
Display the number in place of 0 if possible, otherwise display -1. If there is a matrix of size 1, with an element 0, then print 1.
5 5 5 0
1 2 0 3 1 2 2 3 1
(i)In the first test case the matrix is
Therefore If we place 5 instead of 0, all the element of matrix will become 5. Therefore row 5+5=10, column 5+5=10 and diagonal 5+5=10, all are equal.
(ii)Second sample, it is not possible to insert an element in place of 0 so that the condition is satisfied.thus result is -1.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.