Skip to content

Instantly share code, notes, and snippets.

@lasverg
Last active May 29, 2023 06:34
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save lasverg/3cedfb161512583fc5ce1be1afb18fbd to your computer and use it in GitHub Desktop.
Save lasverg/3cedfb161512583fc5ce1be1afb18fbd to your computer and use it in GitHub Desktop.
Diagonal Difference | Solution | JavaScript
/*
Diagonal Difference Solution.
sample matrix = [[1,2,3], [4,5,6], [7,8,9]]
*/
function diagonalDifference(matrix) {
// length of input matrix.
const length = matrix.length;
let diagonal1 = 0, diagonal2 = 0;
// Looping through the array and summing the diagonals.
for(let i = 0; i < length; i++){
// Calculating the primary diagonal.
diagonal1 += matrix[i][i];
// Reversing the second dimension of array to calculate secondary diagonal.
diagonal2 += matrix[length -1 - i][i]
}
// return absolute difference value.
return Math.abs(diagonal1 - diagonal2);
}
@cobiwave
Copy link

One of the best solutions i've seen, congrats 😃

@rajarshi1
Copy link

a is not defined; a = matrix :/

@trAApex
Copy link

trAApex commented Feb 5, 2019

a is not defined; a = matrix :/

Then you can modify the code a bit, and you solved the problem :)

function diagonalDifference(arr) {
    // length of input array.
    const length = arr.length;
    let diagonal1 = 0, 
         diagonal2 = 0;
    // Looping through the array and summing the diagonals.
    for (let i = 0; i < arr.length; i++) {
        // Calculating the primary diagonal.
        diagonal1 += arr[i][i];
        // Reversing the second dimension of array to calculate secondary diagonal.
        diagonal2 += arr[length - 1 - i][i]
    }
    // return absolute difference value.
    return Math.abs(diagonal1 - diagonal2); 

}

This is the best solution what I have seen and easy to read. Awesome, thank you!

@nravitheja
Copy link

It was easy to read and Under stand. No complex code. great things are always simple ..Good work buddy..cheers.

@ananddeepsingh
Copy link

// Complete the diagonalDifference function below.
function diagonalDifference(arr) {
let sum = 0;
for (var i = 0; i <= arr.length - 1; i++) {
sum += arr[i][i] - arr[i].reverse()[i];
}
return Math.abs(sum);
}
console.log(diagonalDifference(input));

@rahul4coding
Copy link

Thanks dude!

@dev-kalai-m
Copy link

dev-kalai-m commented Jan 9, 2020

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

@lasverg
Copy link
Author

lasverg commented Mar 9, 2020

Hi guys, I'm glad to help you guys out with the solution, and yeah there was type mistake that i made, and couldn't fixed in time, but now I've fixed it. Enjoy and happy coding, peace :)

@rakibulalam
Copy link

return Math.abs(arr.reduce((memo,value,key)=>memo+value[key]-value[value.length-1-key],0));

@Max4n
Copy link

Max4n commented Oct 9, 2020

When I saw this solution, I understand, that it was an easy task

@anthonybj
Copy link

can anyone explain why this code works please?

@kholub1989
Copy link

Great solutions, very easy to read. Thanks😀

@mr-uyghur
Copy link

Great solution, I had similar logic but couldn't figure out the right syntax. this was educational

@roxannecodes
Copy link

Love this solution!! Thanks I was overthinking it way too much lol

@JoshSalway
Copy link

JoshSalway commented May 2, 2022

Alternative solution, just use the same For loop, to loop through in ascending and descending order:

function diagonalDifference(arr) {
    // Write your code here
    let firstDiagonalTotal = 0 
    let secondDiagonalTotal = 0  
    
    // firstDiagonal = arr[0][0] + arr[1][1] + arr[2][2]
    // secondDiagonal = arr[0][2] + arr[1][1] + arr[2][0]
    for (let i = 0, k = arr[0].length - 1; i < arr[0].length; i++, k--) {
        firstDiagonalTotal += arr[i][i]
        secondDiagonalTotal += arr[i][k]
    }

    return Math.abs(firstDiagonalTotal - secondDiagonalTotal)
}

@muhammadnabeelmehmood
Copy link

muhammadnabeelmehmood commented May 7, 2022

Here is the matrix

3
11 2 4
4 5 6
10 8 -12

I've implemented above matrix but I'm getting an error NAN. Can you please let me know what's the issue?
let matrix = [[3],[11,2,4],[4,5,6],[10,8,-12]]
console.log(diagonalDifference(matrix));

Your code isn't implementable for different types of matrix. I think you have to test this code on the different test cases like:

4
-1 1 -7 -8
-10 -8 -5 -2
0 9 7 -1
4 4 -2 1
9
6 6 7 -10 9 -3 8 9 -1
9 7 -10 6 4 1 6 1 1
-1 -2 4 -6 1 -4 -6 3 9
-8 7 6 -1 -6 -6 6 -7 2
-10 -4 9 1 -7 8 -5 3 -5
-8 -3 -4 2 -3 7 -5 1 -5
-2 -7 -4 8 3 -1 8 2 3
-3 4 6 -7 -7 -8 -3 9 -6
-2 0 5 4 4 4 -3 3 0

@lasverg
Copy link
Author

lasverg commented May 11, 2022

The matrix should be n x m, where n = m , if your matrix isnt balanced you need to balance rows in the given matrix.

Please check out the example on below codesandbox link:
https://codesandbox.io/s/diagonal-difference-with-balance-matrix-61725c?file=/src/index.js

Although I dont know that expected outputs of given matixs, have a play around it and let me know if its makes sense or not.

@metaory
Copy link

metaory commented Jun 21, 2022

function diagonalDifference(arr) {
    const {a, b} = arr.reduce((acc, cur, i) => {
        acc.a += arr[i][i]
        acc.b += arr[arr.length - 1 - i][i]
        return acc
    }, {a: 0, b: 0})
    return Math.abs(a - b)
}

@derawi
Copy link

derawi commented Oct 13, 2022

function diagonalDifference(arr) {
    let n = arr.length - 1
    // caluclate the difference in every row 
    // e.g. first row: first - last element, second row: secondElement - (secondLast element)
    arr = arr.map((el,i) => el[i]-el[n-i]).reduce((p,c)=>p+c)

    return Math.abs(arr)
}

@oliveselow
Copy link

Alternative solution, just use the same For loop, to loop through in ascending and descending order:

function diagonalDifference(arr) {
    // Write your code here
    let firstDiagonalTotal = 0 
    let secondDiagonalTotal = 0  
    
    // firstDiagonal = arr[0][0] + arr[1][1] + arr[2][2]
    // secondDiagonal = arr[0][2] + arr[1][1] + arr[2][0]
    for (let i = 0, k = arr[0].length - 1; i < arr[0].length; i++, k--) {
        firstDiagonalTotal += arr[i][i]
        secondDiagonalTotal += arr[i][k]
    }

    return Math.abs(firstDiagonalTotal - secondDiagonalTotal)
}

@oliveselow
Copy link

Like

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment