Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Python3 solution to https://www.hackerrank.com/challenges/diagonal-difference/problem
# test data
first_array = [11, 2, 4]
second_array = [4, 5, 6]
third_array = [10, 8, -12]
sumArray = []
sumArray.append(first_array)
sumArray.append(second_array)
sumArray.append(third_array)
def findDiagonalDifference(array):
primaryDiagonal = []
secondaryDiagonal = []
index = 0
last_index = len(array[0]) - 1
for arrayRow in array:
primaryDiagonal.append(arrayRow[index])
secondaryDiagonal.append(arrayRow[last_index])
last_index -= 1
index += 1
sum_primary = sum(primaryDiagonal)
sum_secondary = sum(secondaryDiagonal)
abs_difference = abs(sum_primary - sum_secondary)
findDiagonalDifference(sumArray)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.