My solution fails for the third test case. Can you please tell what condition I’m missing? The variables choice1 and choice2 refer to the choices made by players 1 and 2. I’ve broken the problem into if-else statements depending on whether player one chooses the first element or last and then player two choosing the first element or the last one
#include
using namespace std;
int score = 0;
void gameStrategy(int* a, int n, int start){
//base case
if(start >= n)
return ;
//recrusive case
int choice1 = max(a[start],a[n-1]);
score += choice1;
int choice2;
if(choice1 == a[start]){
choice2 = max(a[start+1],a[n-1]);
if(choice2 == a[start+1]){
gameStrategy(a,n,start+2);
}
else{
gameStrategy(a,n-1,start+1);
}
}
else{
choice2 = max(a[start],a[n-2]);
if(choice2 == a[start]){
gameStrategy(a,n-1,start+1);
}
else{
gameStrategy(a,n-2,start);
}
}
}
int main() {
int n;
cin >> n;
int a[n];
for(int i = 0 ; i < n ; i++){
cin >> a[i];
}
gameStrategy(a,n,0);
cout << score;
return 0;
}