Given
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\leq N \leq 100\$
\$−100 \leq A_{i} \leq 100\$
Solution
def diagonal_difference(matrix):
l = sum(matrix[i][i] for i in range(N))
r = sum(matrix[i][N-i-1] for i in range(N))
return abs(l - r)
matrix = []
N = input()
for _ in range(N):
matrix.append(map(int, raw_input().split()))
print diagonal_difference(matrix)
Is there more elegant solution for the given problem. Also, I am getting intuition that I can use reduce
, is that possible?