Skip to content

Instantly share code, notes, and snippets.

@xettri
Created March 14, 2022 10:19
Show Gist options
  • Save xettri/9687173c45bd8a671cc7869b00afa138 to your computer and use it in GitHub Desktop.
Save xettri/9687173c45bd8a671cc7869b00afa138 to your computer and use it in GitHub Desktop.
function sumDiagonal(a){
const n = Math.sqrt(a.length);
const op = { d1: 0, d2: 0 };
for(let i = 0; i < n; i++){
op.d1 += a[i + n * i ];
op.d2 += a[n - i - 1 + n * i];
}
return op;
}
const input = [
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
]
console.log(sumDiagonal(input));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment