In this HackerRank Day 11 2D arrays 30 days of code problem, we need to develop a program that can take a 2d array as an input and then print the maximum hourglass sum of that array.

Day 11 2D arrays 30 days of code solution hackerrank


Problem solution in Python 2 programming.      

#!/bin/python

import sys

def countHourglass(arr, i, j):
    hg = 0
    hg += arr[i][j]
    hg += arr[i-1][j-1] + arr[i-1][j] + arr[i-1][j+1]
    hg += arr[i+1][j-1] + arr[i+1][j] + arr[i+1][j+1]
    return hg
   
def getMaxHourglass(arr):
    maxHg = -999999
    for i in range(1, 5):
        for j in range(1, 5):
            hg = countHourglass(arr, i, j)
            maxHg = hg if hg > maxHg else maxHg
    return maxHg
            

arr = []
for arr_i in xrange(6):
   arr_temp = map(int,raw_input().strip().split(' '))
   arr.append(arr_temp)
print getMaxHourglass(arr)
   


Problem solution in Python 3 programming.

#!/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())))
    sum = 0
    tarr = []
    
    for l in range(0,4):
        for k in range(0,4):
            for i in range(l,l+3):
                for j in range(k,k+3):
                    if i == l+1 and ( j == k or j == k+2):
                        continue
                    else:
                        sum += arr[i][j]
            tarr.append(sum)
            sum = 0
    
    print(max(tarr))




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();
            }
        }
        int maxval = -9*6;
        for(int i=0; i<4; i++) {
            for(int j=0; j<4; j++) {
                int sum = arr[i][j] + arr[i][j+1] + arr[i][j+2];
                sum += arr[i+1][j+1];
                sum += arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
                if(sum>maxval) {
                    maxval = sum;
                }
            }
        }
        System.out.println(maxval);
    }
}


Problem solution in c++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;

int add(int arr[6][6], int arr_i, int arr_j){
    int sum = arr[arr_i][arr_j];
    sum += arr[arr_i][arr_j +1];
    sum += arr[arr_i][arr_j +2];
    sum += arr[arr_i +1][arr_j +1];
    sum += arr[arr_i +2][arr_j];
    sum += arr[arr_i +2][arr_j +1];
    sum += arr[arr_i +2][arr_j +2];
    return sum;
}

int main(){
    int sum = -9 * 7;
    int arr[6][6];
    for(int arr_i = 0;arr_i < 6;arr_i++){
       for(int arr_j = 0;arr_j < 6;arr_j++){
          cin >> arr[arr_i][arr_j];
       }
    }
    for(int arr_i  = 0;arr_i < 4;arr_i++){
        for(int arr_j  = 0;arr_j < 4;arr_j++){
            int t = add(arr, arr_i, arr_j);
            if(t > sum)
                sum = t;
        }
    }
    cout << sum << endl;
    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>

int soln(int arr[6][6]) {
    int i, j, sum,temp_sum;
    temp_sum = 0;
    sum = -9999999;
    for(i=0; i<6-2;i++) {
        for(j=0; j<6-2;j++){
            temp_sum = 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];
            //printf("%d %d %d\n", arr[i][j], arr[i][j+1], arr[i][j+2]);
            //printf("  %d   \n", arr[i+1][j+1]);
            //printf("%d %d %d\n", arr[i+2][j] ,arr[i+2][j+1] ,arr[i+2][j+2]);
            //printf("temp sum: %d \n\n", temp_sum);
            if(temp_sum > sum) {
                sum = temp_sum;
            }
        }
    }
    return sum;
}

int main(){
    int arr[6][6];
    for(int arr_i = 0; arr_i < 6; arr_i++){
       for(int arr_j = 0; arr_j < 6; arr_j++){
          scanf("%d",&arr[arr_i][arr_j]);
       }
    }
    printf("%d\n", soln(arr));
    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();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

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

function main() {
    var arr = [];
    for(arr_i = 0; arr_i < 6; arr_i++){
       arr[arr_i] = readLine().split(' ');
       arr[arr_i] = arr[arr_i].map(Number);
    }
    /* row i, column j
     * 1 1 1
     * 1 1 1
     * 1 1 1 
     */
    var arrs = []
    for (var i = 1; i < arr.length - 1;i++){
        for (var j = 1; j < arr[i].length - 1; j++){
            var sum = 0;
            // top
            sum = parseInt(arr[i-1][j-1]) + parseInt(arr[i-1][j]) + parseInt(arr[i-1][j+1]);
            // middle
            sum = sum + parseInt(arr[i][j]);
            // bottom
            sum = sum + parseInt(arr[i+1][j-1]) + parseInt(arr[i+1][j]) + parseInt(arr[i+1][j+1]);
            arrs.push(sum);
        }
    }
    //console.log(arrs);
    console.log(Math.max.apply(null, arrs));

}