2D Matrix Absolute Difference Diagonals Elements Sum - python - Spicy Coders

Recent

Saturday, July 29, 2017

2D Matrix Absolute Difference Diagonals Elements Sum - python

A Square Matrix has N rows and N columns. Get the matrix as input and find the absolute difference between the sum of the values in the two diagonals.

Input Format: The first line contains N. Next N lines containing the elements of the matrix with N column values separated by a space.

Output Format: Absolute difference between the sum of the values in the two diagonals. 

Boundary Conditions: 
  • 1 <= N <= 100 

Example Input/Output 1: 
Input: 
35 80 
90 20 

Output:115 

Example Input/Output 2: 
Input:
1 4 4 2 
2 2 1 8 
8 1 3 5 
10 1 8 3 

Output:
5

Coding:

n=int(input())
m=[[int(i) for i in input().split()]for j in range(n)]
s=sum(m[i][i]for i in range(n))
s1=sum(m[i][n-i-1]for i in range(n))

print(abs(s-s1))

No comments:

Post a Comment