optimal game stratedy ouestion
i am getting coorect output but did not pass all cases
Please check the code
mistake:
ur only checking for the next state from a particular state
u need to check all possibilities
problem 2 is that if
if(arr[s]>arr[e])
{
count+=arr[s];
if(arr[s+1]>arr[e])
{
findmax(arr,s+2,e,n-2);
}
that if s = n-2
it accesses and invalid matrix element
u need to follow this kind of recursion
but this directly will give TLE
you have to use the concept of DP for that
way 1 is memoisation store the result for ever i and n in a 2d matrix and if that substrate is already computed return it
way 2 is a 2d dp formulae
for (int gap = 0; gap < n; ++gap) {
for (int i = 0, j = gap; j < n; ++i, ++j) {
// Here x is value of F(i+2, j), y is F(i+1, j-1) and
// z is F(i, j-2) in above recursive formula
int x = ((i + 2) <= j) ? table[i + 2][j] : 0;
int y = ((i + 1) <= (j - 1)) ? table[i + 1][j - 1] : 0;
int z = (i <= (j - 2)) ? table[i][j - 2] : 0;
table[i][j] = max(arr[i] + min(x, y), arr[j] + min(y, z));
}