Associated Course(s):
Fundamentals of Programming with C

Given a matrix **A[][]** of size **N X N**. The task is to calculate the absolute difference between the sums of its diagonal.

Input: A[][] = 11 2 4

4 5 6

10 8 -12

Output: 15

Sum of primary diagonal = 11 + 5 + (-12) = 4.

Sum of secondary diagonal = 4 + 5 + 10 = 19.

Difference = |19 - 4| = 15.

**Input:**

The first line of input consists of **T**, denoting number of the test cases. Then T testcases follow. The first line of every test case consists of N, denoting the size of matrix. The second line of every test case consists of N*N spaced integers.

**Output:**

For each test case in a single line print an integer denoting the difference between the sum of its diagonals.

**Constraints:**

1 <= T <= 100

1 <= N <= 50

1 <= A[i][j] <= 100

**Example:
Input:**

2

3

11 2 4 4 5 6 10 8 -12

4

1 2 3 4 5 9 7 8 6 10 11 12 13 14 15 16

**Output:**

15

3

Author: saksham seth

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.

I_give_up | 241 |

maggiiiii | 143 |

makeitdonesolo | 140 |

lost_found | 128 |

abhithakur588 | 119 |

Bishnu Dev Panda | 777 |

I_give_up | 552 |

okayboss | 551 |

yk12 | 514 |

makeitdonesolo | 480 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.