Home30 Days Of CodeHackerRank Day 11 : 2D arrays 30 days of code solution

HackerRank Day 11 : 2D arrays 30 days of code solution

Today we are going to solve HackerRank Day 11 : 2D arrays 30 days of code solution in C, C++ , Java , Python & Javascript.

Objective

Today, we are building on our knowledge of arrays by adding another dimension.

Context
Given a 6 x 6 2D ArrayA:

``````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``````

We define an hourglass in A to be a subset of values with indices falling in this pattern in A‘s graphical representation:

``````a b c
d
e f g``````

There are 16 hourglasses in A, and an hourglass sum is the sum of an hourglass’ values.

Calculate the hourglass sum for every hourglass in A, then print the maximum hourglass sum.

Example

In the array shown above, the maximum hourglass sum is 7 for the hourglass in the top left corner.

Input Format

There are 6 lines of input, where each line contains 6 space-separated integers that describe the 2D Array A.

Constraints

• -9 <= A[i][j] <= 9
• 0 <= ij <= 5

Output Format

Print the maximum hourglass sum in A.

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

A contains the following hourglasses:

``````1 1 1   1 1 0   1 0 0   0 0 0
1       0       0       0
1 1 1   1 1 0   1 0 0   0 0 0

0 1 0   1 0 0   0 0 0   0 0 0
1       1       0       0
0 0 2   0 2 4   2 4 4   4 4 0

1 1 1   1 1 0   1 0 0   0 0 0
0       2       4       4
0 0 0   0 0 2   0 2 0   2 0 0

0 0 2   0 2 4   2 4 4   4 4 0
0       0       2       0
0 0 1   0 1 2   1 2 4   2 4 0``````

The hourglass with the maximum sum (19) is:

``````2 4 4
2
1 2 4``````

2D arrays HackerRank 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 soln(int arr[6][6]) {
int i, j, sum,temp_sum;
temp_sum = 0;
sum = -9999999;
for(i=0; i<6-2;i++) {
for(j=0; j<6-2;j++){
temp_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];
//printf("%d %d %d\n", arr[i][j], arr[i][j+1], arr[i][j+2]);
//printf("  %d   \n", arr[i+1][j+1]);
//printf("%d %d %d\n", arr[i+2][j] ,arr[i+2][j+1] ,arr[i+2][j+2]);
//printf("temp sum: %d \n\n", temp_sum);
if(temp_sum > sum) {
sum = temp_sum;
}
}
}
return sum;
}

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]);
}
}
printf("%d\n", soln(arr));
return 0;
}```

2D arrays HackerRank Solution in C ++

```#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;

int add(int arr[6][6], int arr_i, int arr_j){
int sum = arr[arr_i][arr_j];
sum += arr[arr_i][arr_j +1];
sum += arr[arr_i][arr_j +2];
sum += arr[arr_i +1][arr_j +1];
sum += arr[arr_i +2][arr_j];
sum += arr[arr_i +2][arr_j +1];
sum += arr[arr_i +2][arr_j +2];
return sum;
}

int main(){
int sum = -9 * 7;
int arr[6][6];
for(int arr_i = 0;arr_i < 6;arr_i++){
for(int arr_j = 0;arr_j < 6;arr_j++){
cin >> arr[arr_i][arr_j];
}
}
for(int arr_i  = 0;arr_i < 4;arr_i++){
for(int arr_j  = 0;arr_j < 4;arr_j++){
int t = add(arr, arr_i, arr_j);
if(t > sum)
sum = t;
}
}
cout << sum << endl;
return 0;
}```

2D arrays HackerRank 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) {
Scanner in = new Scanner(System.in);
int arr[][] = new int[6][6];
for(int i=0; i < 6; i++){
for(int j=0; j < 6; j++){
arr[i][j] = in.nextInt();
}
}
int maxval = -9*6;
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
int sum = arr[i][j] + arr[i][j+1] + arr[i][j+2];
sum += arr[i+1][j+1];
sum += arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
if(sum>maxval) {
maxval = sum;
}
}
}
System.out.println(maxval);
}
}
```

2D arrays HackerRank Solution in Python 3

```#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__':
arr = []

for _ in range(6):
arr.append(list(map(int, input().rstrip().split())))
sum = 0
tarr = []

for l in range(0,4):
for k in range(0,4):
for i in range(l,l+3):
for j in range(k,k+3):
if i == l+1 and ( j == k or j == k+2):
continue
else:
sum += arr[i][j]
tarr.append(sum)
sum = 0

print(max(tarr))```

2D arrays HackerRank 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();
});

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] = arr[arr_i].map(Number);
}
/* row i, column j
* 1 1 1
* 1 1 1
* 1 1 1
*/
var arrs = []
for (var i = 1; i < arr.length - 1;i++){
for (var j = 1; j < arr[i].length - 1; j++){
var sum = 0;
// top
sum = parseInt(arr[i-1][j-1]) + parseInt(arr[i-1][j]) + parseInt(arr[i-1][j+1]);
// middle
sum = sum + parseInt(arr[i][j]);
// bottom
sum = sum + parseInt(arr[i+1][j-1]) + parseInt(arr[i+1][j]) + parseInt(arr[i+1][j+1]);
arrs.push(sum);
}
}
//console.log(arrs);
console.log(Math.max.apply(null, arrs));

}```

30 Days of Code HackerRank Solutions List – Day 0 to Day 29

RELATED ARTICLES