HACKERRANK HOURGLASS CHALLENGE USING PYTHON
When I saw this question on Hackerrank, I wondered why it was under the easy difficulty level.
Given a 6x6 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:
There are 16 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.
Input Format: There are 6 lines of input, where each line contains 6 space-separated integers that describe the 2D Array A.
Constraints:
Output Format: Print the maximum hourglass sum in A.
Well, upon research, I agree with the difficulty level.
This question asks for the sum of the values in each houglass in matrix A and return the highest.
Hourglass is a subset of the matrix A. It is a 3x3 matrix with only one element on the second row, and this element is located on the second column:
Constraints Explained
Implies that each element is a single digit as it ranges between -9 and 9 inclusive
Implies that i and j range from 0 to 5 inclusive indicating that the number of rows and the number of columns are not more than 6 (since arrays are zero-indexed and a matrix is a type of array)
Labelled Array
Let's number the cells of our array A as follows:
The above 6x6 matrix has 36 elements numbered 1-36
At row[0], column[0], A[0][0], we have 1
At row[0], column[1], A[0][1], we have 2
At row[0], column[2], A[0][2], we have 3
...
At row[1], column[0], A[1][0], we have 7
At row[1], column[1], A[1][1], we have 8
...
At row[2], column[0], A[2][0], we have 13
...
At row[2], column[5], A[2][5], we have 18
...
At row[5], column[0], A[5][0], we have 31
...
At row[5], column[5], A[5][5], we have 36
The Hourglasses
The first hourglass from our numbered array A will be:
thats is
Then...
, ,
, , , ,
...and so on
- We are to programmically get all the hourglasses
- Sum up the elements of each hourglass
- Return the highest sum
Solution
import math
def get_max_hour_glass_sum(arr):
result = -math.inf
for row in range(4):
for col in range(4):
hour_glass_sum = \
arr[row][col] + arr[row][col+1] + arr[row][col+2] + \
arr[row+1][col+1] + \
arr[row+2][col] + arr[row+2][col+1] + arr[row+2][col+2]
result = max((result, hour_glass_sum))
print(result)
In the above code, we import the math
module so we can use it in our function.
- We define our function
get_max_hour_glass_sum
which takesarr
as parameter. - We assign negative infinity (-math.inf) to the
result
variable. Since the question asks for maximum sum, we start with negative infinity and change the value ofresult
to the hourglass sum higher than the currentresult
value. Every time we generate the sum of an hourglass, we check it against the value ofresult
; ifresult
is higher, it remains unchanged, else, the value ofresult
changes to the value of the hourglass sum. - We run a nested for-loop that starts at the first row till the fourth row then the first column till the fourth column. If we went beyond the fourth row or column, we wont be able to acheive an hourglass. For instance, using our numbered matrix above, going to the fifth column gives us:
- We sum the current elements and its neighbours that make up an hourglass and assign the sum to
hour_glass_sum
- We check for the highest between
hour_glass_sum
andresult
and assign it to result - Finally, we print out result.
Please, let me know if there is a more efficient way to achieve the result; I'ld love to learn.
Enjoy!
Thanks to Knowledge Center
Photo by Nathan Dumlao on Unsplash
arr[row][col]+arr[row][col +1]+arr[row][col +2]+
^
SyntaxError: cannot assign to operator
could you explain where did i go wrong
You have no idea how much does this helps me, thanks :).
What is the time and space complexity of your solution?