• + 0 comments

    Here is my soln in JavaScript. I'm sure that there is a more efficient way to do this, but this was quick and easy.

    function hourglassSum(arr) {
        let maxSum = -9999;
        let currSum = 0;
        
        for (let row = 0; row < arr.length - 2; row++) {
            for (let col = 0; col < arr.length - 2; col++) {
                currSum = getHourglassSum(arr, row, col);
                
                if (currSum > maxSum) {
                    maxSum = currSum;
                }
            }
        }
        
        return maxSum;
    }
    
    function getHourglassSum(arr, row, col) {
        return arr[row][col] 
                + arr[row][col + 1] 
                + arr[row][col + 2] 
                + arr[row + 1][col + 1] 
                + arr[row + 2][col] 
                + arr[row + 2][col + 1] 
                + arr[row + 2][col + 2];
    }