We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This solution doesn't work. first, row = input().split() is inside the for loop. input has to be entered in each iteration. second, int(row[i])-int(row[-(i+1)]) is not properly incremented. you are not skipping to the next row.
here is a solution that works (inspired by yours):
N = int(input())
matrix = input().split( )
a = 0
b = -1
total = 0
for i in range(N):
try:
b -= N - 1
total += int(matrix[a]) - int(matrix[b])
a += N + 1
except IndexError:
break
print(abs(total))
you need the exception or you will will get indexError list index out of range.
sadly, my solution still needs to be optimized. it runs in 6.943235158920288 seconds
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Diagonal Difference
You are viewing a single comment's thread. Return to all comments →
This solution doesn't work. first,
row = input().split()
is inside the for loop. input has to be entered in each iteration. second,int(row[i])-int(row[-(i+1)])
is not properly incremented. you are not skipping to the next row.here is a solution that works (inspired by yours):
you need the
exception
or you will will getindexError list index out of range
.sadly, my solution still needs to be optimized. it runs in
6.943235158920288 seconds