Day 11 2D Array Hackerrank Solution in C++ & Java | 30 Days

Write a program to Day 11 Hackerrank 2D Array Hourglass Solution in Java and C++. Array Hourglass Hackerrank Solution. Find the Hackerrank Hourglass Solution in C++ & Java. 30 Days of Code. So basically the problem is clear we have to find the hourglass sum or maximum hourglass sum in a given 2D array. Hourglass is a sum of input in a 2D array those shapes are in 'A' graphical. We can understand this by taking an example of a 2D array.

Below are an example and solutions to this problem. before solving this problem let's first know What is Array?: An array is a group of similar data types, so basically we have to find the maximum sum of A shapes in a Matrix.

Example

The 6*6 array is given below.

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

an array contains the following hourglasses, and we have to find the maximum sum of 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``````

As we can see the above hourglasses' maximum sum is formed by the below hourglass.

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

add all the values 2 + 4 + 4 +2 + 1 + 2 +4 = 19 so this is a solution of the above hourglass.

Tip: Try to solve this problem without using a vector just use a simple array, and try to the modified the solution to this problem.

Day 11 2D Array 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 main() {
vector < vector < int > > arr(6, vector < int > (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];
}
}
int maxsum = -64;
int hoursum;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
hoursum = arr[i + 1][j + 1];
for (int k = 0; k < 3; k++) {
hoursum = hoursum + arr[i][j + k] + arr[i + 2][j + k];
}
if (hoursum > maxsum)
maxsum = hoursum;
}
}
cout << maxsum;
return 0;
}``````

2D Array Hourglass Hackerrank Solution in Java 7

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

Previous Post
Next Post