Find Your Solution Here

Hacker Rank

Hackerrank Solution 2D Array – DS

Problem Link–https://www.hackerrank.com/challenges/2d-array/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays

Solution:

#include<iostream>

using namespace std;
int ar[6][6];
int row[]={-1,-1,-1,1,1,1};
int col[]={-1,0,1,-1,0,1};
void printHourGlassSum()
{
    int res=-100;
    for(int i=1;i<=4;i++){
        for(int j=1;j<=4;j++){
                int ans=ar[i][j];
            for(int k=0;k<6;k++){
                ans+=ar[i+row[k]][j+col[k]];
            }
            res=max(res,ans);
        }
    }
   cout<<res<<endl;

}

int main()
{

    for(int i=0;i<6;i++)
    {
        for(int j=0;j<6;j++){
            cin>>ar[i][j];
        }
    }
    printHourGlassSum();
}

Leave a Reply