HackerRank 2D Array – DS Problem Solution Yashwant Parihar, March 18, 2025March 18, 2025 This post will solve the 2D Array – DS Solution in HackerRank. Given a 6 x 6 2D array, arr. an hourglass is a subset of values with indices falling in the following pattern: a b c d e f g There are 16 hourglasses in a 6 x 6 array. The hourglass sum is the sum of the values in an hourglass. Calculate the hourglass sum for every hourglass in arr, then print the maximum hourglass sum.Examplearr = -9 -9 -9 1 1 1 0 -9 0 4 3 2 -9 -9 -9 1 2 3 0 0 8 6 6 0 0 0 0 -2 0 0 0 0 1 2 4 0 The 16 hourglass sums are: -63, -34, -9, 12, -10, 0, 28, 23, -27, -11, -2, 10, 9, 17, 25, 18 The highest hourglass sum is 28 from the hourglass beginning at row 1, column 2: 0 4 3 1 8 6 6 Note: If you have already solved the Java domain’s Java 2D Array challenge, you may wish to skip this challenge. Function DescriptionComplete the function hourglass Sum with the following parameter(s):int arr[6][6]: a 2-D array of integers Returnsint: the maximum hourglass sum Input FormatEach of the 6 lines of inputs arr[i] contains 6 space-separated integers arr[i][j]. Constraints-9≤ arr[i][j] <90≤ j ≤5 Sample Input1 1 1 0 0 00 1 0 0 0 01 1 1 0 0 00 0 2 4 4 00 0 0 2 0 00 0 1 2 4 0 Sample Output19 HackerRank 2D Array – DS Solution in C #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int arr[6][6]; for(int arr_i = 0; arr_i < 6; arr_i++){ for(int arr_j = 0; arr_j < 6; arr_j++){ scanf("%d",&arr[arr_i][arr_j]); } } int maxNumber = 0; int temp = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (i < 4 && j < 4) { temp = 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]; if (i == 0 && j == 0) { maxNumber = temp; } } if (temp > maxNumber) { maxNumber = temp; } } } printf("%d",maxNumber); return 0; } HackerRank 2D Array – DS Solution in C++ #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],i,j,k,l,x,sum,max; for(i=0;i<6;i++) for(j=0;j<6;j++) cin>>arr[i][j]; k=0; l=0; x=0; sum=0; max=-30; while(x<16){ for(i=l;i<3+l;i=i+2){ for(j=k;j<3+k;j++){ sum+=arr[i][j]; } } sum+=arr[i-3][j-2]; if(sum>max){ max=sum; } sum=0; x++; l=x/4; k=x%4; } cout<<max; return 0; } HackerRank 2D Array – DS Solution in Java import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; 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 scanner = new Scanner(System.in); int[][] in = new int[6][6]; for (int i = 0; i < 6; i++) { for(int j = 0; j < 6; j++) { in[i][j] = scanner.nextInt(); } } int max = Integer.MIN_VALUE; for (int r = 0; r < 4; r++) { for(int c = 0; c < 4; c++) { int s = in[r][c] + in[r][c + 1] + in[r][c + 2] + in[r + 1][c + 1] + in[r + 2][c] + in[r + 2][c + 1] + in[r + 2][c + 2]; if (s > max) { max = s; } } } System.out.println(max); } } HackerRank 2D Array – DS Solution in JavaScript process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var arr = []; for(arr_i = 0; arr_i < 6; arr_i++){ arr[arr_i] = readLine().split(' '); arr[arr_i] = arr[arr_i].map(Number); } var max=-9999; for(i=0;i<4;i++){ for(j=0;j<4;j++){ var temp=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]; if(temp>max) max=temp } } console.log(max); } HackerRank 2D Array – DS Solution in Python #!/bin/python import sys arr = [] for arr_i in xrange(6): arr_temp = map(int, raw_input().strip().split(' ')) arr.append(arr_temp) def hourglass_sum(matrix, row, column): return matrix[row][column - 1] + matrix[row][column] + matrix[row][column + 1] + \ matrix[row + 1][column] + \ matrix[row + 2][column - 1] + matrix[row + 2][column] + matrix[row + 2][column + 1] hourglass_sums = [hourglass_sum(arr, row, column) for row in xrange(4) for column in xrange(1, 5)] print max(hourglass_sums) Other Solutions: HackerRank Dynamic Array Problem Solution HackerRank Solutions Hackerrank Solutions