I was solving this problem: [2D Array][1]
My approach includes brute-force approach of selecting each element and checking it’s neighbouring elements if they are same or not. But somehow my code is giving wrong answer.
Here’s my code:

#include <iostream>
using namespace std;
int main()
{
int a[6][6];
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
cin>>a[i][j];
int sum,ans=0;
for(int i=1;i<5;i++)
{
for(int j=1;j<5;j++)
{
if(a[i][j] != a[i-1][j] || a[i][j] != a[i][j-1] || a[i][j] != a[i-1][j+1] ||
a[i][j] != a[i+1][j-1] || a[i][j] != a[i+1][j] || a[i][j] != a[i+1][j+1])
sum = a[i][j]+a[i-1][j]+a[i][j-1]+a[i-1][j+1]+a[i+1][j-1]+a[i+1][j]+a[i+1][j+1];
ans = max(sum, ans);
}
}
cout<<ans;
return 0;
}

Your loop and your hourglass indices are incorrect. Also no need of inequality check as we only need to find maximum sum hourglass. Its a one liner problem.

I have edited your code and now it’s passing 7 cases out of 7, take a look…
If you initialize ans = 0 your code fails on 3rd and 7th test case. If you take ans = -6 your code passes all test cases but fails on test case no. 7.For ans =-18 you can pass all the test cases.

#include <iostream>
using namespace std;
int main()
{
int a[6][6];
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
cin>>a[i][j];
int sum,ans=-18;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
sum = a[i][j]+a[i][j+1]+a[i][j+2]+a[i+1][j+1]+a[i+2][j]+a[i+2][j+1]+a[i+2][j+2];
ans = max(sum, ans);
}
}
cout<<ans;
return 0;
}

It was a simple problem (saying cause I solved it in a single try). Since the answer is given, I will just give a few tips-

Such problems are simple, but we MUST make sure to thoroughly test the corner test cases. Eg- Your hourglass may work in middle, but is it working correctly for Extreme rows? Extreme columns? (Meaning Row 1, Final Row etc.) . It is a good habit to check your code at corner elements while dealing with such a problem (i.e. 4 corner elements of 2D matrix)

The loop control variable MUST be thought of carefully. Make sure it is not going out of bounds! (Sometimes system doesn’t give out of bound exception, but gives a garbage value during run time. This causes a LOT of confusion as such where the code went wrong)

Make sure you test your code thoroughly. Make sure to brainstorm over corner cases. Because these practice problems are meant to sharpen your skills, algo AND test-case thinking ability. That’s why hackerrank doesn’t give test cases for free. It shows them to you for 5 hackos, which you must earn by correctly solving questions.

If possible, include comments or tell about the approach you’re using while asking. Not necessary, but makes our job easier to see. For example, I didn’t get why you did the inequality check and had to spend some time thinking what your possible approach was. AFAIK you tried to sum the elements only if any one of those was different. What I guess is you wanted to save time. But what my basic instincts tell me (might be wrong) is that it then needs to do 6-7 checks in if else to save 1 operation of calculating sum. I mean, it actually has no significant difference on time. Yes, if that if else had some lengthy loop in it, then your approach would surely save time, but I don’t think inequality check is needed here.