Timer is Running

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<=a_{i}<=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.

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.

visutkarsh2000 | 251 |

ayushaggarwal821 | 248 |

ashishboss9977 | 236 |

tekuripurushotham | 178 |

ramharshrai1999 | 151 |

abnandi04 | 776 |

KshamaGupta | 499 |

ponnadasaikesav | 450 |

ayushaggarwal821 | 415 |

yogesh_aka_dante | 405 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4752 |

Login to report an issue on this page.