• + 0 comments

    Javascript Solution

    function hourglassSum(arr) {
        // Write your code here
        let n = arr.length - 2
        let highest = -Infinity
        for(let i = 0; i < n; i++){
            for(let j = 0; j < n; j++){
                let k = arr[i].slice(j, j + 3).reduce((x, y) => x + y, 0)
                let l = arr[i + 1][j + 1]
                let m = arr[i + 2].slice(j, j + 3).reduce((x, y) => x + y, 0)
                let sum = k + l + m
                
                highest = Math.max(sum, highest)
            }
        }
        return highest
    }