Solving Popular Algorithms: Matrix Diagonal Sums

This is the wrong type of matrix
Example of Matrix Diagonals via LeetCode
mat = [[1,2,3],[4,5,6],[7,8,9]]
diagonalSum = (mat) => {
let primSum = 0

for (let i = 0; i < mat.length; i++) {
primSum += mat[i][i]
}

}
diagonalSum = (mat) => {
let primSum = 0
let secSum = 0
for (let i = 0; i < mat.length; i++) {
primSum += mat[i][i]
secSum += mat[i][mat.length-1-i]
}

}
diagonalSum = (mat) => {
let primSum = 0
let secSum = 0
for (let i = 0; i < mat.length; i++) {
primSum += mat[i][i]
secSum += mat[i][mat.length-1-i]
}

if (mat.length%2 !== 0) {
let mid = Math.floor(mat.length/2)
secSum -= mat[mid][mid]
}

return primSum + secSum
}
Did you know Diagon Alley put together is Diagonally?!?

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Epns AMA with AlphaFinanceLab

Creating Postman Tests for API Requests

Extreme Programming

Transition options to SAP S/4HANA

R-Planet guide to Uplift your land!

Build a Twitter Bot with Node.js and the Pokémon API

3D Render of a pokeball

Hedgehog Team Development for Sitecore (TDS) v5.8 Review

3-D Printed Robotic Arm

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
HopeGiometti

HopeGiometti

More from Medium

My Experience With Debugging

Where Visuals And Algorithms Collide

Guide To Dynamic Programming 👨‍💻

Git Rebase vs Git Merge