• + 0 comments

    Here's my javascript solution:-

    function main() {
    
        let arr = Array(6);
    
        for (let i = 0; i < 6; i++) {
            arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
        }
        
        console.log(maxHourglassSum(arr));
    }
    
    function maxHourglassSum(arr) {
        const rows = arr.length;
        const cols = arr[0].length;
    
        // Handle cases where the array is smaller than 3x3
        if (rows < 3 || cols < 3) return null;
    
        let maxSum = -Infinity; // Initialize to the smallest possible value
    
        // Loop through the top-left corners of the possible hourglasses
        for (let i = 0; i <= rows - 3; i++) {
            for (let j = 0; j <= cols - 3; j++) {
                // Calculate the hourglass sum
                let sum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] // Top row
                        + arr[i + 1][j + 1]                         // Middle element
                        + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2]; // Bottom row
    
                // Update maxSum if the current hourglass sum is greater
                if (sum > maxSum) {
                    maxSum = sum;
                }
            }
        }
    
        return maxSum;
    }