Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix arr
is shown below:
1 2 3
4 5 6
9 8 9
The left-to-right diagonal = 15. The right to left diagonal = 17. Their absolute difference is 2.
Complete the diagonalDifference function in the editor below.
diagonalDifference takes the following parameter:
The first line contains a single integer, n
, the number of rows and columns in the square matrix arr
.
Each of the next lines describes a row, , and consists of space-separated integers .
Return the absolute difference between the sums of the matrix's two diagonals as a single integer.
def diagonalDifference(arr):
# Write your code here
la = len(arr)
a = 0
b = 0
for i in range(la):
a += arr[i][i]
b += arr[i][la-1-i]
return abs(a-b)
left-to-right diagonal 합은 i==j 일 때의 합이고, The right to left diagonal 합은 i+j == len(arr)-1 일 때의 합이다.