Sort by

recency

|

3697 Discussions

|

  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'hourglassSum' function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts 2D_INTEGER_ARRAY arr as parameter.
    #
    
    def hourglassSum(arr):
        arrsum = []
        def hourglass(arr,i,j):
            hg = []
            for n in range(3): 
                if j + n >= len(arr[0]):
                    return []
                else:
                    hg.append(arr[i][j+n])
                    print(arr[i][j+n])
                    
            if i + 1 >= len(arr):
                return []
            else:
                hg.append(arr[i+1][j+1])
                print(arr[i+1][j+1])
            
            if i + 2 < len(arr):
                for n in range(3): 
                    hg.append(arr[i+2][j+n])
            else:
                return []
            return hg
            
        for i in range (len(arr)):
            for j in range (len(arr[0])):                    
                hg =  hourglass(arr,i,j)
                if hg != []:
                    arrsum.append(sum(hg))
                    
                    
        return max(arrsum)
                    
        
                
    
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        arr = []
    
        for _ in range(6):
            arr.append(list(map(int, input().rstrip().split())))
    
        result = hourglassSum(arr)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()
    
  • + 0 comments

    Java code:

    public static int hourglassSum(List> arr) {

    int maxSum = Integer.MIN_VALUE;
    
    for(int i=0;i<4;i++) {
            for(int j=0;j<4;j++) {
                    int sum = arr.get(i).get(j) + arr.get(i).get(j+1) + arr.get(i).get(j+2) 
                                    + arr.get(i+1).get(j+1) 
                            + arr.get(i+2).get(j) + arr.get(i+2).get(j+1) + arr.get(i+2).get(j+2);
    
                    if(sum > maxSum) {
                            maxSum = sum;
                    }
            }
        }
        return maxSum;
    }
    
  • + 0 comments

    C# SOLUTION

    Brief explanation it checks both the row and column count since there could be more than 6 in either, added guards just in case. For the maxHourglassRow and maxHourglassCol I remove the first and last position a long side with one more for indexing giving to a total of 3 hence the maxHourglassRow - 3 (The exercise has 6 numbers, so if I remove 3, I am left with 3 indexing starts with 0 and I want to start counting to sum them i.e. row[0][0], row[0][1], row[0][2] <- the 0, 1 , 2 represent a column in a 2 dimensional array). In the sum I reused a method to sum the 3 columns from the top of the hourglass and the bottom as well, any comment is welcomed. I believe there could be improvements but for now this is what I came up with.

    public static int hourglassSum(List<List<int>> arr)
        {
            int rowCount = arr.Count;
            
            if (rowCount == 0)
            {
                throw new ArgumentException(); 
            }
    				
            int colCount = arr[0].Count;
    				
            if (colCount == 0)
            {
                throw new ArgumentException(); 
            }
    				
            List<int> finalSumArray = [];
            int maxHourglassRow = rowCount - 3;
            int maxHourglassCol = colCount - 3;
            for (var r = 0; r <= maxHourglassRow; r++) {
                for (var c = 0; c <= maxHourglassCol; c++) {
                    List<int> firstRow = arr[r];
                    List<int> secondRow = arr[r + 1];
                    List<int> thirdRow = arr[r + 2];
                    finalSumArray.Add(rowSum(firstRow, c) + secondRow[c + 1] + rowSum(thirdRow, c));
                } 
            }
            return finalSumArray.Max();
    		}
        
        public static int rowSum(List<int> row, int column)
        {
            int secondColumn = column + 1;
            int thirdColumn = column + 2;
            return row[column] + row[secondColumn] + row[thirdColumn];
        }
    
  • + 0 comments

    My solution in JavaScript :-)

    function hourglassSum(arr) {
      let sum = -Infinity;
    
      for (let i = 0; i <= 3; i++) {
        for (let j = 0; j <= 3; j++) {
          let a = arr[i][j];
          let b = arr[i][j + 1];
          let c = arr[i][j + 2];
    
          let d = arr[i + 1][j + 1];
    
          let e = arr[i + 2][j];
          let f = arr[i + 2][j + 1];
          let g = arr[i + 2][j + 2];
    
          if (a + b + c + d + e + f + g > sum) {
            sum = a + b + c + d + e + f + g;
          }
        }
      }
      return sum;
    }
    
  • + 0 comments
    int hourglassSum(vector<vector<int>> arr) {
    vector<int>sums(16);
    
        int t=0;
    for(int i=0;i<16;i++){
       int j=i,c=i%4; 
       if(i%4==0&&i!=0)t++;
            for(int k=c;k<c+3;k++){
                sums[j]+=arr[t][k]+arr[t+2][k];
            }
            sums[i]+=arr[t+1][c+1];
    }
    int maxi=INT_MIN;
    for(int i=0;i<16;i++)if(maxi<sums[i])maxi=sums[i];
    return maxi;
    }