Algorithm

[HackerRank/js] Diagonal Difference

죠죠_ 2022. 7. 5. 13:38

문제 : 

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

For example, the square matrix arr  is shown below:

function diagonalDifference(n, arr) {
    let leftToRigth = 0;
    let rightToLeft = 0;
    
    for (let i = 0; i< arr.length; i++) {
       leftToRigth += arr[i][i];
       rightToLeft += arr[i][(arr.length-1-i)]
    }
    
    return Math.abs(leftToRigth-rightToLeft)
}