Sitemap

Hackerrank Data Structure - 2D Array

3 min readApr 17, 2023

--

Calculate the sum of every hourglass that present in an array

2D Array

Problem

Given a 6*6 2D Array, arr:

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

An hourglass in A is a subset of values with indices falling in this pattern in arr’s graphical representation:

a b c
d
e f g

There are hourglasses in arr. An hourglass sum is the sum of an hourglass’ values. Calculate the hourglass sum for every hourglass in arr , then print the maximum hourglass sum. The array will always be 6x6 .

Example:

arr=

-9 -9 -9  1 1 1 
0 -9 0 4 3 2
-9 -9 -9 1 2 3
0 0 8 6 6 0
0 0 0 -2 0 0
0 0 1 2 4 0

The 16 hourglass sums are:

-63, -34, -9, 12, 
-10, 0, 28, 23,
-27, -11, -2, 10,
9, 17, 25, 18

The highest hourglass sum is 28 from the hourglass beginning at row 1, column 2:

0 4 3
1
8 6 6

Input Format:

Each of the 6 lines of inputs arr[i] contains 6 space-separated integers arr[i][j].

Constraints

-9<=arr[i][j]<=9
0<=i,j<=5

Output Format

Print the largest (maximum) hourglass sum found in arr.

Get Arthi Murali’s stories in your inbox

Join Medium for free to get updates from this writer.

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

19

Explanation

arr contains the following hourglasses:

Press enter or click to view image in full size

The hourglass with the maximum sum (19) is:

2 4 4
2
1 2 4

Solution :

array = []
for a in range(6):
Input = [int(x) for x in str(input()).split(' ')]
array.append(Input)
maximum = -9*7
for i in range(6):
for j in range(6):
if i + 2 < 6 and j + 2 < 6:
res = array[i][j]+array[i][j+1]+array[i][j+2]+array[i+1][j+1]+array[i+2][j]+array[i+2][j+1]+array[i+2][j+2]
if res>maximum:
maximum = res
print(maximum)

An empty list is assigned to a variable array.

for loop iterates for 6 times, taking 6 rows as input, with each row containing a string of numbers separated by spaces, which are then converted to integers and stored in variable Input. Input is then appended to the variable array.

9*7 is assigned to variable maximum. -9 is least number from the constraint, and 7 is the total number of values in an hourglass.

The loop iterates through each row and iterates through integers in individual rows.

i and j will iterate till 3 ,after which any number added to 2 will give a number greater or equal to 6.

And then, based on the i and j values, the elements in the specific position are taken , and an hourglass pattern is formed.

For example, i = 0 and j = 0, The hour glass pattern is ,

1 1 1
1
0 1 0

And at the end, all the sums of the hourglass pattern with different i and j values are added together , the answer is stored in a variable res.

If res is greater than the maximum (-63), then res is assigned to the variable maximum.

maximum is printed.

I hope this blog helps. Do let me know if there is any feedback or any other approach to this problem. Would love to learn :)

--

--

Arthi Murali
Arthi Murali

Written by Arthi Murali

Data science enthusiast | Data engineering | Biomedical Science Graduate | Chemoinformatics