Missing number in matrix
Submissions: 1593   Accuracy:

7.87%

  Difficulty: Easy   Marks: 2

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.

Input:
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.

Output:
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.

Constraints:

1<=T<=25
1<=n<=100
1<=ai<=1000000000


Example:
Input

2
2
5 5 5 0
3
1 2 0 3 1 2 2 3 1

Output:
5
-1

Explanation:

(i)In the first test case the matrix is

5 5
5 0

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.                                                                                              

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: vaibhav garg
Author: justrelax


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.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.