JavaScript Algorithm: Diagonal Difference

0 31
Avatar for buzz.lightyear
3 years ago

In today’s algorithm, we will be creating a function called diagonalDifference. For this function you will receive an input that is a multi-dimensional array. A multi-dimensional array is an array containing arrays.

An in this function you will calculate and return the absolute difference between the sums of its diagonals.

If you look at the post image above, think of the input array as a tic tac toe grid. Each item in the array will contain an array of three values. Each row, you calculate the blue and pink circles in that row. In other words, in the example array:

[[1,2,3], [4,5,6], [7,8,9]]

You can also arrange the array to be easier to read and resemble a tic tac toe grid:

[
[1,2,3],
[4,5,6],
[7,8,9]
]

The diagonal going from left-to-right would be 1,5 and 9. The diagonal going right-to-left would be 3, 5, 7. The end goal is to take the sum of the left-to-right diagonal and subtract it from the sum of the right-to-left diagonal. Remember, you return the absolute difference so if the total becomes a negative number, you get the absolute value of that number and return the result.

Let’s turn this into code.

We create a series of variables:

let forwardIndex = 0;
let backwardsIndex = arr.length - 1;
let forwardCount = 0;
let backwardsCount = 0;

The forwardIndex variable will act as an index for each array to grab the numbers in the left-to-right diagonal.

The backwardsIndex variable will act as an index for each array to grab the numbers in the right-to-left diagonal. Since we are starting from the end of the array, we get the length of the array and subtract one (computers start counting at zero, not one).

The fowardCount variable will hold the sum of the left-to-right diagonal.

The backwardsCount variable will hold the sum of the right-to-left diagonal.

Next, is to use a for-loop to loop through the input array:

for(let i = 0; i < arr.length; i++){
    forwardCount += arr[i][forwardIndex];
    backwardsCount += arr[i][backwardsIndex];    
    forwardIndex++;
    backwardsIndex--;
}

On the first line inside this for-loop, we loop through each array inside the main array. Our forwardCount variable begins by grabbing the first item while the backwardsCount variable grabs the last item.

As the for-loop loops through each array, the forwardIndex increments. It is working from left-to-right. On the first array it grabs the first value of the first array, then the second value of the second array, etc. On the other hand, the backwardsIndex, decrements. It is working backwards from right-to-left and stepping down its value for each array.

To get access to a value in a multi-dimensional array, you use [i][j] bracket notation. An example is arr[1][2]. The first bracket means the number is in an array that is the second item in the arr array. The second bracket means the number is in the third item within that second array.

The values within the left-to-right diagonal are added to the forwardCount each time. The same happens for the values within the right-to-left diagonal to the backwardsCount variable.

After the for-loop ends, the final thing to do is to subtract the results of forwardCount from backwardsCount. Since you are returning the absolute value of that result, you will use the Math.abs() method.

return Math.abs(forwardCount - backwardsCount);

And that is it.

Here is the rest of the code:

function diagonalDifference(arr) {    
    let forwardIndex = 0;
    let backwardsIndex = arr.length - 1;    
    let forwardCount = 0;
    let backwardsCount = 0;    
    for (let i = 0; i < arr.length; i++) {
        forwardCount += arr[i][forwardIndex];
        backwardsCount += arr[i][backwardsIndex];        
        forwardIndex++;
        backwardsIndex--;
    }    
    return Math.abs(forwardCount - backwardsCount);
}

1
$ 0.00
Avatar for buzz.lightyear
3 years ago

Comments