HackerRank Day 11 Solution in Python: 2D Arrays

Today we will see the HackerRank Day 11 Solution in Python. The problem is named 2D Arrays which is part of 30 Days of code on HackerRank. Let’s get started!

Day 11: 2D Arrays Problem statement

We are given a 2D Array, we have calculated the sum of each hourglass-like pattern and finally, print the maximum sum value.

Sample Hourglass pattern 

a b c
e f g

If our input array is a 6X6 matrix and there are 16 hourglasses in that, an hourglass sum is the sum of hourglass values.

Sample Input

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0

Sample Output

Explanation: The hourglass with the maximum sum is 19. The hourglass with the maximum sum is given below.

2 4 4
1 2 4

You can solve the problem here.

HackerRank Day 11 Solution in Python


import math
import os
import random
import re
import sys

if __name__ == '__main__':
    #List to store input
    arr = []
    #Get input as 2D list
    for i in range(6):
        arr.append(list(map(int, input().rstrip().split())))
    #List to store sum of hourglass
    #Iterate through the list and store sum of each hourglass in sum_array
    for i in range(4):
        for j in range(4):
    #Prints the maximum sum

Code Explanation:

  • We get the input 2D array elements as a list named arr
  • Then, we create a list sum_array to store the sum of each hourglass in the 2D array.
  • We iterate through the input list to calculate and store the sum of the hourglass.
  • Finally, we print the maximum value in the sum_array, which denotes the maximum sum of the hourglass in the given input 2D array.

Also Read:

Avatar of Keerthana Buvaneshwaran

Author: Keerthana Buvaneshwaran