Last active
December 11, 2017 12:34
-
-
Save Asatelit/49c942446da44d59a2100aa19d19f1a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Solution for HackerRank > Algorithms > Warmup > Diagonal Difference | |
https://www.hackerrank.com/challenges/simple-array-sum/diagonal-difference | |
See explanation in Russian at https://www.coursera.org/learn/algoritmizacija-vychislenij/lecture/zinYi/opriedielieniie-indieksov-eliemientov-matritsy-raspolozhiennykh-na-nad-i-pod | |
*/ | |
(function solve(n, matrix) { | |
const diagonals = [0, 0]; | |
for (let i = 1; i < n + 1; i += 1) { | |
diagonals[0] += matrix[i - 1][i - 1]; // sum across the primary diagonal | |
diagonals[1] += matrix[i - 1][n - i]; // sum across the secondary diagonal | |
} | |
return Math.abs(diagonals[1] - diagonals[0]); // difference (absolute value) | |
})(3, [[11, 2, 4], [4, 5, 6], [10, 8, -12]]); | |
/* | |
// convert array into matrix (ECMAScript 6 style) | |
const matrix = ar.reduce( | |
(rows, key, index) => (index % n === 0 ? rows.push([key]) : rows[rows.length - 1].push(key)) && rows, | |
[] | |
); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment