Not able to understand why test case failed

Hi @kushal1998
Test cases are failing because when you are computing vi then you are considering only one case that if first person take ith array element then second will take jth array element, but there is possibility that second person will take i+1th element. So instead of using
long long int vi=a[i]+optimalGame(a,i+1,j-1);
use should use
long long int vi=a[i]+min(optimalGame(a,i+1,j-1),optimalGame(a,i+2,j));

Similarly you have to makes changes for vj.

Here is your corrected code :

If your doubt is clear then mark it as resolved.

i am not able to complete understand the second case could you explain by dry running??
like there are elements 1 2 3 4 if i have selected 1 then the another person will have chance to select 2 or 4 only right? what is i+1th element you mentioned ?? and the use of minimum function you have used.

Try this testcase :
4
2 3 7 4

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.

1 Like

oops such a typical test case … thank for referring to a video

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.