# HackerRank Diagonal Difference solution with JavaScript

Let's learn how to solve the Diagonal Difference challenge from the HackerRank website.

Given a square matrix, we must calculate the absolute difference between the sums of its two diagonals. Consider the following example of a 3x3 matrix.

```
1 2 3
4 5 6
9 8 9
```

The sum of the left-to-right diagonal is `1+5+9 = 15`

. The sum of the right-to-left diagonal is `3+5+9 = 17`

. The absolute difference of the two is `15 - 17 = 2`

.

To solve this problem, the function that we must define will receive a multi-dimensional array as input. Each inner array in the outer array will represent a row of the matrix.

## Pseudo code solution

- Create a
`rightDiagonalSum`

variable to store the sum of the left-to-right diagonal. - Create a
`leftDiagonalSum`

variable to store the sum of the right-to-left diagonal. - Get the matrix size by taking the length of the multi-dimensional array.
- Loop over the length of the multi-dimensional array.
- Compute the
`rightDiagonalSum`

by using the loop counter to reference the row and column indices for the left diagonal. - Compute the
`leftDiagonalSum`

by using the loop counter and the matrix size to reference the row and column indices for the right diagonal.

- Compute the
- Compute the absolute value of the difference between both diagonal sums.
- Return the absolute value.

## Code solution

```
function diagonalDifference(arr) {
let rightDiagonalSum = 0;
let leftDiagonalSum = 0;
const matrixSize = arr.length;
for(let i = 0; i < matrixSize; i++) {
rightDiagonalSum += arr[i][i];
leftDiagonalSum += arr[i][(matrixSize - 1) - i];
}
return Math.abs(rightDiagonalSum - leftDiagonalSum);
}
```

Consider a 3x3 matrix. Let's see what happens during each loop iteration.

**When i is 0**

`arr[0][0]`

, the top-left number in the matrix, is added to`rightDiagonalSum`

,`arr[0][2]`

, the top-right number in the matrix, is added to`leftDiagonalSum`

,

**When i is 1**

`arr[1][1]`

, the center number in the matrix, is added to`rightDiagonalSum`

,`arr[1][1]`

, the center number in the matrix, is added to`leftDiagonalSum`

,

**When i is 2**

`arr[2][2]`

, the bottom-right number in the matrix, is added to`rightDiagonalSum`

,`arr[2][0]`

, the bottom-left number in the matrix, is added to`leftDiagonalSum`

,

## Test cases

Let's test the `diagonalDifference`

function above. For the first test case, we will use the very first matrix we defined above. The absolute difference between the two diagonals should be `2`

.

```
const arr = [[1, 2, 3], [4, 5, 6], [9, 8, 9]];
console.log(diagonalDifference(arr)); // 2
```

The `diagonalDifference`

function returns the correct value of `2`

for the above test case.

The next test case will use the following matrix.

```
11 2 4
4 5 6
10 8 -12
```

The diagonal difference of this matrix should be `15`

.

```
const arr = [[11, 2, 4], [4, 5, 6], [10, 8, -12]];
console.log(diagonalDifference(arr)); // 15
```

The `diagonalDifference`

function returns the correct value of `15`

for the above test case.

## Time and space complexity

The Big O time complexity of the solution is linear, or `O(n)`

, where `n`

consists of the number of rows in the square matrix. The loop iterates over `n`

rows, represented by the array length assigned to `matrixSize`

.

The space complexity of the solution is constant, or `O(1)`

. Regardless of the size of the input, we'll always require the same numeric variables to be stored in memory.