We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
NEW
- Certify
- Compete
- Hiring developers?
- Prepare
- Data Structures
- Arrays
- 2D Array - DS
- Discussions
Problem
Submissions
Leaderboard
Discussions
Editorial
RishC
6 years ago+ 75 commentsPython3 answer
sum = [] for i in range[len[arr]-2]: for j in range[len[arr]-2]: sum.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]] print[max[sum]]
You are viewing a single comment's thread. Return to all comments →
Homeinterview prepration kitHackerRank 2D Array - DS problem solution
In this HackerRank 2D Array - DS interview preparation kit problem you have to Calculate the hourglass sum for every hourglass arr, then print the maximum hourglass sum. The array will always be 6 x 6.
Problem solution in Python programming.
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the hourglassSum function below.
def hourglassSum[arr]:
maxSum = -63
for i in range[4]:
for j in range[4]:
# sum of top 3 elements
top = sum[arr[i][j:j+3]]
# sum of the mid element
mid = arr[i+1][j+1]
# sum of bottom 3 elements
bottom = sum[arr[i+2][j:j+3]]
hourglass = top + mid + bottom
if hourglass > maxSum:
maxSum = hourglass
return maxSum
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[]
Problem solution in Java Programming.
import java.io.*; import java.util.*; public class Solution { public static void main[String[] args] { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner[System.in]; int[][] array = new int[6][6]; for [int y = 0; y < 6; y++]{ for [int x =0; x