# Hacker Rank Solution For Day 11: 2D Arrays

Problem:- Write a Hackerrank Solution For Day 11: 2D Arrays or Hacker Rank Solution Program In C++ For " Day 11: 2D Arrays " or Hackerrank 30 days of code Java Solution:Day 11: 2D Arrays solution or Hackerrank solution for 30 Days of Code Challenges or Hackerrank 30 days of code Java Solution, Day 11: 2D Arrays solution, or C/C++ Logic & Problem Solving: Day 11: 2D Arrays.

Logic:- So basically the problem is clear we have to find hourglass sum or maximum hourglass sum in a given 2D array. Hourglass is a sum of input in 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 solution to this problem. before solving this problem lets 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 below hourglass.

2 4 4
2
1 2 4

add all the value 2 + 4 + 4 +2 + 1 + 2 +4 = 19 so this is a solution of above hour glass.

All solution provided here are in C++ (CPP) if any reader wants these solutions in C, and Java comments below or sends an email with your query like " day n solution in C / C++ / Java. Check the end of the post solutions with the full explanation.

Important:- If any readers want to write a post for me or for our Programming community click here. You have a better solution and you want to publish your solution send the solution I will publish your post / Article with your name and one more thing you can add 2 backlinks + you can earn credits, this will help you if you have any website and blog.

Copy the colored code and paste it into hacker rank editor. If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about new post this Article.

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

Solution:-

#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;
}

Output:-

You May Also Like:-

1. Hackerrank Solution For Day 12: Inheritance

2. Hackerrank Solution For Day 13: Abstract Classes

3. Hackerrank Solution For Day 14: Scope

4. Hackerrank Solution For Day 15: Linked List

5. Hackerrank Solution For Day 16: Exceptions - String to Integer
Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.

1. Hi, i want to ask for an explanation for this bloc in the code if want, i don't understand it well.
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];
}

1. first for dry run purpose or to get the index of positions we wish to find sum of then read and dry run the above code and you'll get to see that following nested loops run to get the index of positions as of the hourglass.
Thankyou

2. sir can you explain this code?