https://www.hackerrank.com/challenges/2d-array/problem?isFullScreen=true

#include
using namespace std;
int hourglass(int arr [][6],int r,int c)
{
if(r<=6&&c<=6)
{
int sum=0;
for(int row=r;row<r+2;row++)
{
for(int col=c;col<=c+2;col++)
{
if((col==c&&row==r+1)||((col==c+2)&&row==r+1))
{
continue;
}
else {
sum+=arr[row][col];
}
}
}
static int max=0;
if(sum>max)
{
max=sum;
}
if(r+3==5&&c+3==5)
{
return max;
}
int answer1=hourglass(arr,r,c+1);
int answer2=hourglass(arr,r+1,c);
int answer3=hourglass(arr,r+1,c+1);
if(answer1>answer2)
{
if(answer1>answer3)
{
return answer1;
}
else {
return answer3;
}
}
else {
if(answer2>answer3)
{
return answer2;
}
else {
return answer3;
}
}
}

}
int main()
{
int arr[6][6];
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
cin>>arr[i][j];
}
}
int answer=hourglass(arr,0,0);
cout<<answer;
}
plzz tell what is wrong in the code

bro.
U went wrong at many few points, There are cases where the entire hour glass in composed of -ve no,
so return INT_MIN in those case instead of keeping static int max = 0 keep it INT_MIN’

then u could keep the max as a global variable, updating a diff sum variable in each recursive call, and maintaining max of both values

the recursive call for r+1 , c+1 is not required
Here is an updated version oof ur code

maam could you please tell what is the significance of line 28

Basically my thought process was that if we are moving to the 4th row then there need not be any computation done , so i returned INT_MIN ( considering the hour glass has all negative value) .

BUT even if u do not return INT_MIN at that point the function is returning SUM which would give the max_sum so far.
My logic would put the statement like max{sum , INT_MIN, fn(arr, r , c+1))
not return INT_MIN at that point would compute max{sum , sum , fn(arr, r , c+1)}

SO basically to be on the safer side i was returning INT_MIN, but its fine if u don`t. Since SUM is being returned which has the max value so far.

Hope i clarified ur doubt.