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
d
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
19
Explanation: The hourglass with the maximum sum is 19. The hourglass with the maximum sum is given below.
2 4 4
2
1 2 4
You can solve the problem here.
HackerRank Day 11 Solution in Python
#!/bin/python3 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 sum_array=[] #Iterate through the list and store sum of each hourglass in sum_array for i in range(4): for j in range(4): sum_array.append(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]) #Prints the maximum sum print(max(sum_array))
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:
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- HackerRank Day 8 Solution in Python: Dictionaries and Maps
- HackerRank Day 7 Solution in Python: Arrays
- HackerRank Day 6 Solution in Python: Let’s review
- HackerRank Day 5 Solution in Python: Loops
- HackerRank Day 4 Solution in Python: Class vs Instance
- HackerRank Day 3 Solution in Python: Intro to Conditional Statements
- HackerRank Day 2 Solution in Python: Operators
- HackerRank Day 1 Solution in Python: Data Types
- HackerRank Day 0 Solution in Python: Hello World
- HackerRank Day 29 Solution in Python: Bitwise AND
- HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases
- HackerRank Day 27 Solution in Python: Testing
- HackerRank Day 26 Solution in Python: Nested Logic
- HackerRank Day 25 Solution in Python: Running Time and Complexity
- HackerRank Day 24 Solution in Python: More Linked Lists
- HackerRank Day 23 Solution in Python: BST Level Order Traversal
- HackerRank Day 22 Solution in Python: Binary Search Trees
- Find Peak Element LeetCode 162
- HackerRank Day 20 Solution in Python: Sorting
- HackerRank Day 19 Solution in Python: Interfaces
- HackerRank Day 18 Solution in Python: Queues and Stacks
- HackerRank Day 17 Solution in Python: More Exceptions
- HackerRank Day 16 Solution: Exceptions – String to Integer
- Explained: Allocate minimum number of pages
- HackerRank Day 15 Solution in Python: Linked List
- Search a 2D matrix: leetcode 74
- Maximum Subarray Sum: Kadane’s Algorithm
- HackerRank Day 13 Solution in Python: Abstract Classes
- HackerRank Day 14 Solution in Python: Scope