Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Day 11 2D arrays 30 days of code solution

YASH PAL, 31 July 202419 October 2025

HackerRank Day 11 2D Arrays solution – 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.

Objective
Today, we are building on our knowledge of arrays by adding another dimension.

Context
Given a 6 x 6 2D Array, A:

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

We define an hourglass in A to be a subset of values with indices falling in this pattern in A’s graphical representation:

a b c
  d
e f g

There are 6 hourglasses in A, and an hourglass sum is the sum of an hourglass’ values.

Task
Calculate the hourglass sum for every hourglass in A, then print the maximum hourglass sum.

Example

In the array shown above, the maximum hourglass sum is 7 for the hourglass in the top left corner.

Input Format

There are 6 lines of input, where each line contains 6 space-separated integers that describe the 2D Array A.

Constraints

  • -9 <= A[i][j] <= 9
  • 0 <= i,j<= 5

Output Format

Print the maximum hourglass sum in A.

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 %dn", arr[i][j], arr[i][j+1], arr[i][j+2]);
            //printf("  %d   n", arr[i+1][j+1]);
            //printf("%d %d %dn", arr[i+2][j] ,arr[i+2][j+1] ,arr[i+2][j+2]);
            //printf("temp sum: %d nn", 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("%dn", 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));

}

30 days of code coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes