Why this solution is giving Memory Limit Exceeded error


#1

bool is_possible(vector a,int x,int &flag)
{
if(x<0)
{
return false;
}

if(x>=(a.size()-1))
{

    flag =1;
    return true;
}

if(a[x]==0 && x!=a.size()-1)
{   
  //  cout<<"yes2"<<endl;
    return false;
}

int p = a[x];
int max1 = a[x]+x;
int index = x;
for(int u=1;u<=p;u++)
{
int i = a[x+u];
i = u+x+i;
// cout<<i<<" “<<x<<” "<<x+u<<endl;
if(i>max1)
{
max1 = i;
index = x+u;
}
}

if(max1 == a[x]+x && max1<a.size()-1)
{
return false;
}
if(is_possible(a,index,flag))
{
return true;
}

return false;

}

int Solution::canJump(vector &a)
{
int flag = 0;
int x=0;
bool p = is_possible(a,x,flag);

return flag;

}