Sort by

recency

|

3706 Discussions

|

  • + 0 comments

    Easy to read JAVA solution:

    public static int hourglassSum(List<List<Integer>> arr) {
    
        int arrLength = 6;
        int subArrLength = 6;
        int hourGlassHeight = 3;
        int maxSum = Integer.MIN_VALUE;
        int lastTopRow = arrLength - hourGlassHeight;
    
        for (int i = 0; i <= lastTopRow; i++) {
            for (int topCenter = 1; topCenter < subArrLength - 1; topCenter++) {
                List<Integer> topRow = arr.get(i);
                List<Integer> midRow = arr.get(i + 1);
                List<Integer> botRow = arr.get(i + 2);
    
                int topRowSum = (
                    topRow.get(topCenter - 1) +
                    topRow.get(topCenter) +
                    topRow.get(topCenter + 1)
                );
                int midRowSum = midRow.get(topCenter);
                int botRowSum = (
                    botRow.get(topCenter - 1) +
                    botRow.get(topCenter) +
                    botRow.get(topCenter + 1)
                );
                int currentSum = topRowSum + midRowSum + botRowSum;
    
                if (currentSum > maxSum) maxSum = currentSum;
            }
        }
        return maxSum;
    }
    
  • + 0 comments

    C++11.

    We need to:

    Traverse a 6x6 2D array.

    For each possible “hourglass” (shape of 7 cells), calculate its sum.

    Track and return the maximum sum.

    Here’s a clean solution:

    include

    using namespace std;

    int hourglassSum(vector> arr) { int maxSum = INT_MIN; // since values can be negative

    for (int i = 0; i <= 3; i++) {
        for (int j = 0; j <= 3; j++) {
            int sum = arr[i][j] + arr[i][j+1] + arr[i][j+2]   // top row
                    + arr[i+1][j+1]                           // middle
                    + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]; // bottom row
    
            maxSum = max(maxSum, sum);
        }
    }
    return maxSum;
    

    }

    int main() { vector> arr(6, vector(6)); for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { cin >> arr[i][j]; } } cout << hourglassSum(arr) << endl; return 0; }

    ✅ Explanation:

    We only go up to index 3 (not 5), since an hourglass needs 3 rows and 3 columns.

    At each (i, j), we compute the hourglass sum.

    Keep updating maxSum.

    Return the largest sum found.

    🔹 For the sample input in your problem, this outputs 19.

  • + 0 comments
      let max = -Infinity;
        for(let i=0;i<=3;i++){   
            for(let j= 0;j<=3;j++){
                
            let  sum= arr[i][j]+arr[i][j+1]+arr[i][j+2]
                          +arr[i+1][j+1]+
                    arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]
                         
                if(sum>max){
                    max=sum
                }
            }
        }
        return max
         
    
  • + 0 comments

    max_sum = -float("inf")

    for i in range(4):
        for j in range(4):
            top = arr[i][j]+arr[i][j+1]+arr[i][j+2]
            middle = arr[i+1][j+1]
            bottom = arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]
            total = top + middle + bottom
            max_sum = max(max_sum, total)
    return max_sum
    
  • + 0 comments

    The 2D Array – DS problem tests how you handle multi-dimensional arrays efficiently, especially for pattern extraction like hourglass sums. In the same way, planning an Umrah from Montréal requires organizing multiple elements (flights, hotels, guidance) in a structured way, ensuring everything fits together smoothly for the best experience.The 2D Array – DS problem tests how you handle multi-dimensional arrays efficiently, especially for pattern extraction like hourglass sums. In the same way, planning an Umrah from Montréal requires organizing multiple elements (flights, hotels, guidance) in a structured way, ensuring everything fits together smoothly for the best experience.