We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
letfirstDiagonalSum=0letsecDiagonalSum=0// The first coordinate for the first diagonal should be at the begin of the first row (arr[0][0])letnextDiagX=0// The first coordinate for the second diagonal should be at the end of the first row (arr[0][arr.length - 1])letnextDiagY=arr.length-1for(letrow=0;row<arr.length;row++){for(letcol=0;col<arr.length;col++){if(col===nextDiagX)firstDiagonalSum+=arr[row][col]if(col===nextDiagY)secDiagonalSum+=arr[row][col]}// After each row iteration we should update the coordinate for each diagonal, so in the next iteration we can get the proper valuesnextDiagX+=1nextDiagY-=1}returnMath.abs(firstDiagonalSum-secDiagonalSum)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Diagonal Difference
You are viewing a single comment's thread. Return to all comments →
My Solution in Typescript