Optimal Game Strategy-1 failing testcases


My code is failing for 2 test cases, please help.

Hey @yashjain0112 for input
12
50 75 52 96 89 33 20 94 95 50 71 3
Expected output is: 377
yours is giving: 351

Use this

if (i>j) return 0; // this should be your base case
int op1 = a[i] + min(f(a, i + 2, j), f(a, i + 1, j - 1));
int op2 = a[j] + min(f(a, i + 1, j - 1), f(a, i, j - 2));
return max(op1, op2);
  • Here your i will be from (n-1, 0)
    and j will be from (0,n-1)
  • as we are taking one from backward and one from front.
1 Like

Have sent you my personalized code in chat with comments in it for better understanding.

But in question it says that, ith index coin has i value!
Hence my code…

They are given n coins with values x1, x2 …. xn where ‘n’ is always even. They take alternate terms. In each turn, a player picks either the first coin or the last coin from the row and removes it from the row. The value of coin is received by that player. Determine the maximum value that Piyush can win with if he moves first. Both the players play optimally.
See the bold part, you will get the intuition

1 Like

Cant find the code in chat, please send it again , thanks

Have send it again, check one more time

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.