Diagonal Difference

  • + 0 comments

    My Typescript solution:

    function diagonalDifference(arr: number[][]): number {
        let ltrDiagonalSum = 0;
        let rtlDiagonalSum = 0;
        const length = arr.length;
        
        for (let i = 0; i < length; i++) {
            ltrDiagonalSum += arr[i][i];
            rtlDiagonalSum += arr[i][length - 1 - i];
        }
        
        return Math.abs(ltrDiagonalSum - rtlDiagonalSum);
    }