Optimal Game Strategy -1

1 out of 3 test cases failed.
please help.

Hi @vivek21,
Since both players play optimally this means, both will choose the maximum coins available to them.
Now, see this test case:
4
4 15 3 2
Case 1: Piyush->4, Nimit -> 15 , Piyush ->3 , Nimit ->2 ; Answer = 7
Case 2: Piyush -> 2, Nimit ->4 , Piyush ->15 , Nimit ->3 ; Answer = 17
In case 2 Piyush can have maximum 17 coins.

Your code will give 7 as answer. But the correct answer will be 17.
Try to implement a recursive solution or use a 2D Matrix approach.

but in case 2, why will piyush chose 2 even 4 was bigger than 2.

Hi @vivek21,
This is because we have to choose the maximum coins available for Piyush.
In case 1 if Piyush chooses 4, Nimit will take 15 and have more number of coins than Piyush. See, we don’t have to consider individual moves. We have to see at the end of the game, Piyush should’ve more number of coins.

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.