Sort by

recency

|

1924 Discussions

|

  • + 0 comments

    best solution arr.append(list(map(int, input().rstrip().split()))) maxSum = - 63 for i in range(4): for j in range(4): hourglassSum = (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]) maxSum = max(maxSum, hourglassSum) print(maxSum)

  • + 0 comments

    in c#

    List> arr = new List>();

        for (int i = 0; i < 6; i++)
        {
            arr.Add(Console.ReadLine().TrimEnd()
                        .Split(' ')
                         .ToList()
                         .Select(arrTemp => Convert.ToInt32(arrTemp))
                         .ToList());
        }
    
       List<List<int>> result = new List<List<int>>();
       int indexNumber=0;
       int lenght_arr=arr[0].Count();
    
       for(int i=0; i<=3;i++)
       {
           for(int j=0; j<= lenght_arr-3;j++)
           {
              if(result.Count <= indexNumber) 
              {
                  result.Insert(indexNumber,new List<int>());
              }
    
               result[indexNumber].AddRange(arr[i].Skip(j).Take(3).ToList());
               result[indexNumber].AddRange(arr[i+1].Skip(j+1).Take(1).ToList());
               result[indexNumber].AddRange(arr[i+2].Skip(j).Take(3).ToList());
    
               indexNumber++;
           }
    
       }
    
       int maxnumber=result.Select(x=>x.Sum()).Max();
    
           Console.WriteLine(maxnumber);
    
  • + 0 comments

    in java

    import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.regex.*;

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        List<List<Integer>> arr = new ArrayList<>();
    
        for (int i = 0; i < 6; i++) {
            String[] arrRowTempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
            List<Integer> arrRowItems = new ArrayList<>();
    
            for (int j = 0; j < 6; j++) {
                int arrItem = Integer.parseInt(arrRowTempItems[j]);
                arrRowItems.add(arrItem);
            }
    
            arr.add(arrRowItems);
        }
        int m, max = Integer.MIN_VALUE;
        for (int i=0;i<4;i++)
        {
          for (int j=0;j<4;j++)
          {
            m=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) ;
            max=Math.max(m,max);
            }
        }
        System.out.println(max);
    
        bufferedReader.close();
    }
    

    }

  • + 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;
    }
    
  • + 0 comments

    In C++

    int main() {

    vector<vector<int>> arr(6);
    
    int maxHourGlassSum = -63;
    
    for (int i = 0; i < 6; i++) {
        arr[i].resize(6);
    
        string arr_row_temp_temp;
        getline(cin, arr_row_temp_temp);
    
        vector<string> arr_row_temp = split(rtrim(arr_row_temp_temp));
    
        for (int j = 0; j < 6; j++) {
            int arr_row_item = stoi(arr_row_temp[j]);
    
            arr[i][j] = arr_row_item;
        }
    }
    
    for(int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
    
            int hourGlassSum = 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];
    
            maxHourGlassSum = max(maxHourGlassSum, hourGlassSum);
        }
    }
    
    cout << maxHourGlassSum;
    
    return 0;
    

    }