Optimal Game Strategy-1

What’s wrong in my code?

#include
#include <bits/stdc++.h>
using namespace std;

int profit(int*arr, int i, int j){
if(i > j){
return 0;
}
if(i==j){
return arr[i];
}
int sum = 0;
if(arr[i] > arr[j]){
sum = arr[i];
i++;
}
else{
sum = arr[j];
j–;
}
if(arr[i] > arr[j]){
i++;
}
else{
j–;
}
sum = sum + profit(arr, i, j);
return sum;
}

int main() {
int n;
cin>>n;
int arr[n];
for(int iter=0; iter<n; iter++){
cin>>arr[iter];
}
cout<<profit(arr, 0, n-1);
return 0;
}

share code in cb ide

needs to be done this way

Why are you adding min instead of max?

see the video from course
in Dynammic programming section
name: optimal game strategy
https://online.codingblocks.com/app/player/16084/content/99823/1033/lecture

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.