In this HackerRank Day 11: 2D Arrays problem solution Calculate the hourglass sum for every hourglass in A, then print the maximum hourglass sum.

## Problem solution in Python programming.

#!/bin/python3

import sys

arr = []
for arr_i in range(6):
arr_t = [int(arr_temp) for arr_temp in input().strip().split(' ')]
arr.append(arr_t)

for alist in arr:
alist = list(map(int, alist))

for row in range(4):
for set in range(4):
hourglass = sum(arr[row][set:set + 3]) + arr[row + 1][set + 1] + sum(arr[row + 2][set:set + 3])

### Problem solution in Java programming.

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

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int arr[][] = new int[6][6];
for(int i=0; i < 6; i++){
for(int j=0; j < 6; j++){
arr[i][j] = in.nextInt();
}
}
System.out.println(getMaxSum(arr));
}

private static int getMaxSum( int arr[][]){
int row = arr.length;
int col = arr[0].length;

int sum = Integer.MIN_VALUE;
for(int i=0; i+2< row; i++){

for(int l =0; l+2< col; l++){
int count = l;
int temp = 0;
int z = 0;
while(count < (l+3)){
int k = i;
int a = arr[k][count];
int b =0;
if(z == 1){
b = arr[k+1][count];
}
int c = arr[k+2][count];
temp += (a+b+c);
count++;
z++;

}
if(temp > sum){
sum = temp;

}
}
}
return sum;
}
}

### Problem solution in C++ programming.

#include <climits>
#include <cstdio>

#define DIM 6

int main() {
int i, j, sum, max_sum;
int a[DIM][DIM];

for(i = 0; i < DIM; ++i)
for(j = 0; j < DIM; ++j)
scanf("%d", &a[i][j]);

sum = max_sum = INT_MIN;
for(i = 1; i < DIM - 1; ++i)
for(j = 1; j < DIM - 1; ++j) {
sum = a[i - 1][j - 1] + a[i - 1][j] + a[i - 1][j + 1]
+ a[i][j]
+ a[i + 1][j - 1] + a[i + 1][j] + a[i + 1][j + 1];
if(max_sum < sum)
max_sum = sum;
}

printf("%d\n", max_sum);
return 0;
}

### Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

// Use with caution. It won't check for array size, so parameters must be pased with caution.
int hourglassSum(int arr[][6], int i, int j) {
int a;
int sum = 0;

/*
fprintf(stderr, "%d %d %d\n  %d\n%d %d %d\n",
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]);
*/

for(a = 0; a < 3; a++) {
sum += arr[i][j+a];
sum += arr[i+2][j+a];
}

sum += arr[i+1][j+1];
//fprintf(stderr, "Sum: %d\n\n", sum);
return sum;
}

int main(){
int i, j, sum;
int max = -63;
int arr[6][6];

for(i = 0; i < 6; i++)
for(j = 0; j < 6; j++)
scanf("%d", &arr[i][j]);

// Get trough all 16 possible start position (top-left) of hour glasses, and calculate sum
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
sum = hourglassSum(arr, i, j);
if(sum > max)
max = sum;
}
}

printf("%d\n", max);
return 0;
}

### Problem solution in Javascript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
input_stdin += data;
});

process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});

return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
var arr = [];
var x = 0;
for(arr_i = 0; arr_i < 6; arr_i++){
arr[arr_i] = arr[arr_i].map(Number);
}

for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
answer[x] = 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];
x++;
}
}