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.

Problem ( Hacker Rank ) :

Objective
Today, we’re building on our knowledge of Arrays by adding another dimension. Check out the Tutorial tab for learning materials and an instructional video!

Context
Given a  2D Array, :

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  to be a subset of values with indices falling in this pattern in ‘s graphical representation:

a b c
  d
e f g

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

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

Input Format

There are  lines of input, where each line contains  space-separated integers describing 2D Array ; every value in  will be in the inclusive range of  to .

Constraints

Output Format

Print the largest (maximum) hourglass sum found in .

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

 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 () is:

2 4 4
  2
1 2 4

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

Leave a Reply

Your email address will not be published.