is my logic correct??
Optimal game strategy problem
You are close but your conditions for selecting are not right. This is how you need to do it.
ll optimalGame(ll i,ll j){
if(i > j){
return 0;
}
// Consider both the possibilities. You can pick either the first or the last coin.
// Since the opponent plays optimally , we would get the minimum of the remaining coins for each choice.
ll pickFirst = coins[i] + min( optimalGame(i+2,j) , optimalGame(i+1,j-1) ) ;
ll pickLast = coins[j] + min( optimalGame(i,j-2) , optimalGame(i+1,j-1) ) ;
// Pick the max of two as your final result
ll ans = max(pickFirst,pickLast);
return ans;
}
int main() {
int n;
cin >> n;
for(int i=0;i<n;i++){
cin >> coins[i];
}
cout << optimalGame(0,n-1);
return 0;
}