Skip to content# Diagonal Difference code challenge

### Problem

### Input format

### Output format

### Returns

### Solution

### Appendix

## Newsletter

— 1 min read

My approach to solving HackerRank’s Diagonal Difference code challenge.

Given a square matrix, calculate the absolute difference between the sums of its diagonals.

For example, the square matrix is shown below:

`11 2 324 5 639 8 9`

The left-to-right diagonal = `1 + 5 + 9 = 15`

. The right to left diagonal = `3 + 5 + 9 = 17`

. Their absolute difference is `|15-17| = 2`

.

The first line contains a single integer, `n`

, the number of rows and columns in the square matrix `arr`

.

Each of the next `n`

lines describes a row, `arr[i]`

, and consists of `n`

space-separated integers `arr[i][j]`

.

Return the absolute difference between the sums of the matrix's two diagonals as a single integer.

Returns `int`

value of the absolute diagonal difference.

This was a reasonably exciting challenge to solve. I've created empty arrays to store primary (`pri`

) and secondary (`sec`

) diagonal values.

Then I created an `arrSum`

function, which we will use later, to sum up, all the array values using the `Array.reduce()`

method.

Finally, here we have a `for`

loop where all the fun starts to kick in. Let's return to our original example and review our two-dimensional array.

`11 2 324 5 639 8 9`

Following the example above, our primary diagonal will have `1 + 5 + 9`

, while the secondary diagonal will be `9 + 5 + 3`

. Okay, what now? We must access a 2D array - `arr[i][j]`

. Where `[i]`

represents the row's index, while `[j]`

is the element's index within the selected row.

Below is the two-dimensional visualisation of our initial example.

`1[0][0] [0][1] [0][2]2[1][0] [1][1] [1][2]3[2][0] [2][1] [2][2]`

Primary diagonal consists of 1 (`[0][0]`

) + 5 (`[1][1]`

) + 9 (`[2][2]`

).
Secondary diagonal consists of 9 (`[2][0]`

) + 5 (`[1][1]`

) + 3 (`[0][2]`

).

The idea is to understand the pattern. For the primary diagonal, we can see that we start at `[0][0]`

, and we increment both numbers by `1`

with each step.

Below is an example of our `for`

loop for the primary diagonal.

`1for (let i = 0; i < arr.length; i++) {2 pri.push(arr[i][i]);3}`

For the secondary diagonal, things are a bit different. We need to descend, so we start at the last index (`arr.length - 1`

). In our case, it will be `2`

. Then with each iteration, we also need to subtract `- i`

as we descend.

Below is an example of our `for`

loop at this stage.

`1for (let i = 0; i < arr.length; i++) {2 pri.push(arr[i][i]);3 sec.push(arr[arr.length - 1 - i][i]);4}`

Now it's time to calculate sums of primary and secondary diagonal values computed using the earlier created `arrSum`

function and subtract the secondary from the primary. Finally, we can return the result of the `Math.abs()`

where we passed the substracted value of both arrays.

Solution complexity: `O(n)`

.

```
1function diagonalDifference(arr) {2 let pri = [],3 sec = [];4 const arrSum = (arr) => arr.reduce((a, b) => a + b);5
6 for (let i = 0; i < arr.length; i++) {7 pri.push(arr[i][i]);8 sec.push(arr[arr.length - 1 - i][i]);9 }10
11 return Math.abs(arrSum(pri) - arrSum(sec));12}
```

Sign up to get updates when I write something new. No spam ever.

Subscribe to my Newsletter