Saturday, 1 April 2017

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.

Hacker Rank Solution For 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 of 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 a mail 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. 

Recently Updated Post:-

1. Hackerrank Solution For Day 6: Let's Review

2. Hackerrank Solution For Day 7: Arrays

3. Hacker Rank Solution For Day 8: Dictionaries and Maps

4. Hacker Rank Solution For Day 9: Recursion

5. Hacker Rank Solution For Day 10: Binary Numbers

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 with out using vector just use a simple array, and try to the modified the solution to this problem.

Submit Your Solution Here:- Click Here

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


Hacker Rank Solution For Day 11: 2D Arrays

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

6. Hackerrank Solution For Day 17: More Exceptions

7. Hackerrank Solution For Day 18: Queues and Stacks

8. Hackerrank Solution For Day 19: Interfaces

9. Hackerrank Solution For Day 20: Sorting

10. Hackerrank Solution For Day 21: Generics

No comments:
Write comments

Recommended Posts × +