Diagonal Difference

  • + 1 comment

    in JavaScript:

    function diagonalDifference(arr) {
        const n = arr.length;
        let i = 0;
        let j = 0;
        let k = n - 1;
        let leftDiag = 0;
        let rightDiag = 0;
    
        while (i < n && j < n) {
            leftDiag += arr[i][j];
            rightDiag += arr[i][k];
            i++;
            j++;
            k--;
        }
        
        return Math.abs(leftDiag - rightDiag);
    }