-
Notifications
You must be signed in to change notification settings - Fork 1
/
Diagonal Difference.py
84 lines (56 loc) · 1.6 KB
/
Diagonal Difference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'''
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix is shown below:
1 2 3
4 5 6
9 8 9
The left-to-right diagonal = . The right to left diagonal = . Their absolute difference is .
Function description
Complete the function in the editor below. It must return an integer representing the absolute diagonal difference.
diagonalDifference takes the following parameter:
arr: an array of integers .
Input Format
The first line contains a single integer, , the number of rows and columns in the matrix .
Each of the next lines describes a row, , and consists of space-separated integers .
Constraints
Output Format
Print the absolute difference between the sums of the matrix's two diagonals as a single integer.
Sample Input
3
11 2 4
4 5 6
10 8 -12
Sample Output
15
'''
#Solution
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the diagonalDifference function below.
def diagonalDifference(arr):
ln = len(arr)
rowlen = len(arr[0])
sum1 =0
count =0
sum2=0
for j in range (rowlen):
sum1 = sum1 + arr[j][count]
count = count + 1
count =ln -1
for j in range (rowlen):
sum2 = sum2 + arr[count][j]
count = count - 1
return abs (sum1-sum2)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
arr = []
for _ in range(n):
arr.append(list(map(int, input().rstrip().split())))
result = diagonalDifference(arr)
fptr.write(str(result) + '\n')
fptr.close()