I want to check whether my recursive solution is correct or not if it is correct give me the way (state) to optimise it further.
int dp[2000001];
int a[2000001];
int get(int index,int n,int prev,int k){
if(index>=n)
return 0;
int ans=0;int last=min(index+k,n);
for(int i=index;i<last;i++){
if(prev==-1 || abs(prev-i)!=k){
ans=max(ans,a[i]+get(last,n,i,k));
}
}
return ans;
}
void solve()
{
int n;int k;
cin>>n>>k;
for(int i=0;i<n;i++)cin>>a[i];
memset(dp,-1,sizeof(dp));
cout<<get(0,n,-1,k)<<"\n";
}
Top Down Optimisation
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.