Created
September 27, 2015 15:06
-
-
Save BastinRobin/5139e24b7986a58e3c82 to your computer and use it in GitHub Desktop.
Diagonal Difference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '''Problem Statement | |
| You are given a square matrix of size N×N. Calculate the absolute difference of the sums across the two main diagonals. | |
| Input Format | |
| The first line contains a single integer N. The next N lines contain N integers (each) describing the matrix. | |
| Constraints | |
| 1≤N≤100 | |
| −100≤A[i]≤100 | |
| Output Format | |
| Output a single integer equal to the absolute difference in the sums across the diagonals. | |
| Sample Input | |
| 3 | |
| 11 2 4 | |
| 4 5 6 | |
| 10 8 -12 | |
| Sample Output | |
| 15 | |
| Explanation | |
| The first diagonal of the matrix is: | |
| 11 | |
| 5 | |
| -12 | |
| Sum across the first diagonal = 11+5-12= 4 | |
| The second diagonal of the matrix is: | |
| 4 | |
| 5 | |
| 10 | |
| Sum across the second diagonal = 4+5+10 = 19 | |
| Difference: |4-19| =15 | |
| ''' | |
| def diag_sum(l): | |
| x,y = [], [] | |
| for i in range(len(l)): | |
| x.append(l[i][i]) | |
| y.append(l[i][(len(l) - 1) - i]) | |
| return [x, y] | |
| a = [] | |
| for i in range(input()): | |
| a.append(map(int, raw_input().split())) | |
| print abs(sum(diag_sum(a)[0]) - sum(diag_sum(a)[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment