MariaSzubski
7/21/2016 - 9:22 PM

Given a square matrix of size N x N, calculate the absolute difference between the sums of its diagonals. #hackerrank #warmup

Given a square matrix of size N x N, calculate the absolute difference between the sums of its diagonals. #hackerrank #warmup

/*
Solution for HackerRank > Algorithms > Warmup > Diagonal Difference
https://www.hackerrank.com/challenges/diagonal-difference
*/

function main() {
    var n = 3;
    var a = [[11,2,4],[4,5,6],[10,8,-12]];
    
    // Set counters to '0'
    var diagTop = 0, diagBottom = 0, diff;
    
    // Loop through the arrange and add up the Top-to-bottom and Bottom-to-top values
    for ( var i = 0; i < a.length; i++){
        diagTop += a[i][i];
        diagBottom += a[a.length-1-i][i];
    }
    
    // Find the absolute difference between the two diagonals
    diff = Math.abs(diagTop - diagBottom);
    
    console.log(diff);
}

main();