Sort by

recency

|

1937 Discussions

|

  • + 0 comments

    import java.util.Scanner;

    public class HourglassSum { public static void main(String[] args) { Scanner sc = new Scanner(System.in);

        int[][] arr = new int[6][6];
    
        // Input 6x6 array
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                arr[i][j] = sc.nextInt();
            }
        }
    
        int maxSum = Integer.MIN_VALUE;
    
        // Traverse possible hourglass centers (i from 1 to 4, j from 1 to 4)
        for (int i = 1; i < 5; i++) {
            for (int j = 1; j < 5; j++) {
                int sum = 
                    arr[i - 1][j - 1] + arr[i - 1][j] + arr[i - 1][j + 1] + // top
                    arr[i][j] +                                             // center
                    arr[i + 1][j - 1] + arr[i + 1][j] + arr[i + 1][j + 1];  // bottom
    
                if (sum > maxSum) {
                    maxSum = sum;
                }
            }
        }
    
        System.out.println(maxSum);
        sc.close();
    }
    

    }

  • + 0 comments

    python 3

    !/bin/python3

    import math import os import random import re import sys

    if name == 'main':

    arr = []
    
    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))
    results=[]
    for i in range(0,4):
        for j in range(0,4):
            r=(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])     
            results.append(r)       
    print(max(results))
    
  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    
    if __name__ == '__main__':
        a = []
        for _ in range(6):
            a.append(list(map(int, input().rstrip().split())))
        row=len(a)
        R=[]
        for i in range(row-2):
            for j in range(row-2):
                R.append(a[i][j]+a[i][j+1]+a[i][j+2]+a[i+1][j+1]+a[i+2][j]+a[i+2][j+1]+a[i+2][j+2])
        print(max(R))
    
  • + 0 comments

    python 3

    row= len(arr) p=row-2 n=[]

    while len(n)<=(p*p):
        for i in range(p):
            for j in range(p):
    
                summ=(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])
    
                n.append(summ)
    
    print(max(n))
    
  • + 0 comments

    JS

    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));
        }
        
        var hgSum = -9 * 7; //smallest possible hourglass sum
        
        //loop through the lines until the hourglass is reaching the bottom
        for (let l = 0; l < arr.length-2; l++){
            const arrLine = arr[l];
            
            //loop through the line until the top of the hourglass reaches the end of the line
            for (let i = 0; i < arr[l].length - 2; i++){
                var hourglass = arrLine.slice(i,i+3); //hourglass top
                hourglass.push(arr[l+1][i+1]); //hourglass waist
                hourglass.push(...arr[l+2].slice(i,i+3));//hourglass bottom
                // console.log(hourglass);
                
                var tempSum = hourglass.reduce((a,b) => a + b);
                // console.log(tempSum);
                if (tempSum > hgSum){
                    hgSum = tempSum;
                    }
                }
            }
            console.log(hgSum);
        }