Sort by

recency

|

1928 Discussions

|

  • + 0 comments

    C

    int main() {

    int** arr = malloc(6 * sizeof(int*));
    
    for (int i = 0; i < 6; i++) {
        *(arr + i) = malloc(6 * (sizeof(int)));
    
        char** arr_item_temp = split_string(rtrim(readline()));
    
        for (int j = 0; j < 6; j++) {
            int arr_item = parse_int(*(arr_item_temp + j));
    
            *(*(arr + i) + j) = arr_item;
        }
    }
    
    int countk = 0;
    int countl = 0;
    int sum = 0;
    int sum_max = -63;
    int k = 0;
    int l = 0;
    
    for (int i = 0; i < 4; i++) {
    
        for (int j = 0; j < 4; j++) {
                countk = 0;
                for (k=i; k < i+3; k++) {
                    countl = 0;
                    countk++;
                    for (l=j; l < j+3; l++) {
                        countl++;
                        if(countk==2){
                            if (countl==2) {
                            sum+= *(*(arr + k)+l);
                            }
                        }else {
                            sum+= *(*(arr + k)+l);
                        }         
                    }//end for l
                }//end for k
            if(sum>sum_max){
                sum_max=sum;
            }
            sum=0;    
        }//end for j
    }//end for i 
    
    printf("%i",sum_max);
    
    return 0;
    

    }

  • + 0 comments

    🐍 If you want to solve in your favorite IDE - you can copy """Sample Input""" there and make 2D array from it

    sample_input = """0 -4 -6 0 -7 -6 -1 -2 -6 -8 -3 -1 -8 -4 -2 -8 -8 -6 -3 -1 -2 -5 -7 -4 -3 -5 -3 -6 -6 -6 -3 -6 0 -8 -6 -7"""

    arr = [list(map(int, line.split())) for line in inpuT.split("\n")]

  • + 0 comments

    FOR JAVA

    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<>();
    
        IntStream.range(0, 6).forEach(i -> {
            try {
                arr.add(
                    Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                        .map(Integer::parseInt)
                        .collect(toList())
                );
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });
    
        int answer = Integer.MIN_VALUE;
         for (int i = 0; i <= 3; i++) { 
            for (int j = 0; j <= 3; j++) { 
                int sum = 0;
    
                    sum += arr.get(i).get(j);     
                    sum += arr.get(i).get(j + 1); 
                    sum += arr.get(i).get(j + 2); 
    
                    sum += arr.get(i + 1).get(j + 1);
    
                    sum += arr.get(i + 2).get(j);     
                    sum += arr.get(i + 2).get(j + 1); 
                    sum += arr.get(i + 2).get(j + 2); 
    
                    answer = Math.max(sum, answer);
            }
        }
        System.out.println(answer);
    
        bufferedReader.close();
    }
    

    }

  • + 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);
    
  • + 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)