• + 0 comments

    JavaScript Solution

    const maxLength = arr.length;
    let max = -Infinity;
    let arrAux = Array(3);
    let sum = 0;
    
    const getHourglassSum = (arr) => {
      let sum = 0;
      for (let i = 0; i < 3; i++) {
        for (let j = 0; j < 3; j++) {
          sum += arr[i][j];
        }
      }
      sum = sum - arr[1][0] - arr[1][2];
      return sum;
    };
    
    for (let i = 0; i < maxLength - 2; i++) {
      for (let j = 0; j < maxLength - 2; j++) {
        arrAux[0] = arr[i].slice(j, j + 3);
        arrAux[1] = arr[i + 1].slice(j, j + 3);
        arrAux[2] = arr[i + 2].slice(j, j + 3);
        sum = getHourglassSum(arrAux);
        if (sum > max) max = sum;
      }
    }
    
    console.log(max);