MASIGNASUKAv102
6510051498749449419

Find diagonal Difference of a matrix python [ hacker rank ]

Find diagonal Difference of a matrix python [ hacker rank ]
Add Comments
Tuesday, September 21, 2021


 

Find diagonal Difference of a matrix python

  Sample matrix:

3
11 2 4
4 5 6
10 8 -12

Diagonal 1 : 11 , 5 ,-12
Diagonal 2: 4 ,5 ,10
Difference = |4-19|


Solution:


#!/bin/python3

import math
import os
import random
import re
import sys


def diagonalDifference(arr):
    # Write your code here
    
    p = sum(list(map(lambda x: x[arr.index(x)], arr)))
    q = sum(list(map(lambda x: x[(len(arr) - 1) - arr.index(x)], arr)))
    
    total = abs(p-q)
    
    return total
            

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())

    arr = []

    for _ in range(n):
        arr.append(list(map(int, input().rstrip().split())))

    result = diagonalDifference(arr)

    fptr.write(str(result) + '\n')

    fptr.close()
Adarshreddy Adelli

As an Engineering Lead with deep expertise in Artificial Intelligence, Cybersecurity, and Systems Architecture, I guide teams in building innovative, secure, and scalable solutions.I am passionate about tackling challenging technical problems, fostering engineering excellence, and delivering solutions that balance innovation, security, and performance. I actively share knowledge through blogging and community engagement, contributing to the advancement of AI and cybersecurity practices.