2D Array HackerRank Solution-Looking for 2D Array solution for Hackerrank problem? Get solution with source code and detailed explainer video

Given 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

Go to problem statement

Explanation Video:

Youtube Channel partner: CodingCart
Mentor: Satyendra Jaiswal
Langauge: Python

Source Code:  for hourglassSum function


# Complete the hourglassSum function below.
def hourglassSum(arr):
    m=[]
    for i in range(len(arr)-2):
        for j in range(len(arr)-2):
            m.append(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]]))
    return max(m)             
coming soon…
coming soon…

Full Source Code: 2D Array Hourglass Sum


#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the hourglassSum function below.
def hourglassSum(arr):
    m=[]
    for i in range(len(arr)-2):
        for j in range(len(arr)-2):
            m.append(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]]))
    return max(m)        

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    arr = []

    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))

    result = hourglassSum(arr)

    fptr.write(str(result) + '\n')

    fptr.close()



coming soon…
coming soon…

Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our Youtube Channel.

This post is in partner with CodingCart.