I was solving this problem: 2D Array My approach includes bruteforce 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:
Please Help! asked 23 Feb '17, 20:18

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. My solution for reference. int main(){
register int i,j;
for(i=0;i<6;i++) { for(j=0;j<6;j++) { cin>>a[i][j]; } } for(i=0;i<4;i++) { for(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]; maxsum=max(maxsum,sum); } } cout <<maxsum; return 0; } answered 23 Feb '17, 20:40

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.
answered 23 Feb '17, 20:59

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
answered 23 Feb '17, 21:19
