String game theory

help me
#include
using namespace std;

static int x=1;
void sum1(int a[],int i,int j){

static int sum=0;
if(i>=j){
cout<<sum<<endl;
return ;
}
cout<<a[i]<<" “<<a[j]<<” “<<” ----->"<<x<<" ";
if(x==1){

if(a[i]>a[j])
{
	sum=sum+a[i];
	cout<<"sum "<<sum<<" ";
	x=0;
	sum1(a,i+1,j);
	}
else {
	sum=sum+a[j];
	x=0;
	sum1(a,i,j--);

	}	
	
}

else
{
if(a[i]>a[j]){
cout<<a[i]<<" ";
x=1;
sum1(a,i+1,j);
}
else {
x=1;
sum1(a,i,j–);
}

}

}

int main(){
int n;
cin>>n;
int i,a[n];
for(i=0;i<n;i++){
cin>>a[i];
}
sum1(a,0,n-1);
return 0;
}

Hi @Aug19APPPP0003
You are employing a greedy technique. That is , checking only for local maximum and not aiming for the global maximum.

Try this testcase :
4
2 3 7 4

Using your greedy technque ,
Player 1 picks 4
Player 2 picks 7
Player 1 picks 3
Player 2 picks 2

Player 1 Score = 4 + 3 = 7
Player 2 Score = 7 + 2 = 9

Since our goal was to make sure that Player 1 wins, the greedy technique fails

If we were not inclined to use the greedy technique and had played optimally like this
Player 1 picks 2 (Even though its lesser of 2 & 4)
Player 2 picks 4
Player 1 picks 7
Player 2 picks 3

Player 1 Score = 2 + 7 = 9
Player 2 Score = 4 + 3 = 7

Player 1 wins.
This problem has been discussed in detail in the last video of the Dynamic Programming section with its recursive function and detailed explanation.
Please refer to it.

can u send me link to vedio