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.

- Practice
- Algorithms
- Warmup
- Diagonal Difference
- Discussions

# Diagonal Difference

# Diagonal Difference

djgarrick + 0 comments I went for a non-array approach. Feedback much appreciated :)

`for(int j = 0; j < numInputs; j++){ for(int k = 0; k < numInputs; k++){ cin >> curInput; if(j == k){ leftD += curInput; } if(j+k == (numInputs-1)){ rightD += curInput; } } } ans = abs(leftD-rightD);`

Gamanoid + 0 comments My solution is to accumulate the mirrored difference in each row like so:

`N = int(input()) total = 0 for i in range(N): row = input().split() total += int(row[i])-int(row[-(i+1)]) print(abs(total))`

the only conversions are at two indeces per row regardless of size.

KMH92 + 0 comments The beauty of python:

d1 = sum([a[x][x] for x in range(n)]) d2 = sum([a[x][n-1-x] for x in range (n)]) print(abs(d1-d2))

websash + 0 comments The input description is not clear at all! If you say it's an array of integers one would assume it's an array of numbers of type integer, something like [42, 66, -5, ...] I had to unlock the solution to find out there are 2 split() functions which operate on strings in JS. WTF! It's not an array of integers but array of strings. Come on! Give proper input description

karmasakshi + 0 comments I see people using nested loops to solve this. I think it is not necessary. Here's my JS code:

`var lr = rl = 0; for(var i = 0; i<n; i++){ lr += a[i][i]; rl += a[i][n-i-1]; } console.log(Math.abs(lr-rl));`

Load more conversations

Sort 3578 Discussions, By:

Please Login in order to post a comment