HackerRank 2D Array – DS problem solution YASH PAL, 31 July 2024 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<6; x++){ array[x][y] = sc.nextInt(); } } int maxHourglass = getHourglass(array, 1,1); for (int y=1; y<5; y++){ for (int x=1; x<5; x++){ int hourres = getHourglass(array, x, y); if (hourres > maxHourglass){ maxHourglass = hourres; } } } System.out.println(maxHourglass); } public static int getHourglass(int[][] array, int x, int y) { return array[x][y] + array[x-1][y-1] + array[x][y-1] + array[x+1][y-1] + array[x-1][y+1] + array[x][y+1] + array[x+1][y+1]; } } Problem solution in C++ programming. #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int arr[6][6]; int row, col; int sum; int best_sum; int first_flag=1; for(row=0; row<6; row++){ for(col=0; col<6; col++){ cin >> arr[row][col]; } } for(row=0; row<=3; row++){ for(col=0; col<=3; col++){ 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]; if(first_flag==1){ first_flag=0; best_sum=sum; } else if(sum>best_sum) best_sum=sum; } } cout << best_sum; return 0; } Problem solution in C programming. #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int a[6][6]; int main() { int i,j,sum; int max=-999999; for(i=0;i<6;i++) for(j=0;j<6;j++) scanf("%d",&a[i][j]); for(i=0;i<4;i++) for(j=0;j<4;j++){ sum=a[i][j]+a[i][j+1]+a[i][j+2]+a[i+1][j+1]+a[i+2][j]+a[i+2][j+1]+a[i+2][j+2]; if(sum>max) max=sum; } printf("%d",max); /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; } Problem solution in JavaScript programming. function getSum(arr, row, col) { var i=0, j=0, sum=0; for (i=0; i<3; i++) { for (j=0; j<3; j++) { if ((i===1 && j===0) || (i===1 && j===2)) { sum += 0; } else { sum += arr[row+i][col+j]; } } } return sum; } function processData(input) { var input_arr = input.trim().split('n'); var max = -9999; // console.log(input_arr); // Make a two dimensional array for( var i=0; i<input_arr.length; i++) { input_arr[i] = input_arr[i].split(' ').map(function(n){ return parseInt(n); }) } // console.log(input_arr); for (var i=0; i<4; i++) { var temp = 0; for (var j=0; j<4; j++) { temp = getSum(input_arr, i, j); // console.log(max, temp); if (max <= temp) max = temp; } } console.log(max); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); coding problems interview prepration kit