Optimal Game strategy -1 ouput explanation

In the given problem , from the coins in input: 1 2 3 4, Piyush first picks 4. Then it is written in output explanation that Nimit picks either 1 or 3, and in both cases piyush will pick 2. Why so? If nimit picks 1, Piyush can pick 3 right?

Both players can pick only from the two ends. And they pick optimally.
When it is piyush’s turn, he will pick such a number such that Nimit ultimately loses.
Similary on Nimit’s turn, he will pick such a number such that Piyush ultimately loses.

Follow this
At each instance we would need to consider two possibilities that we can pick the first as well as the last element of the remaining array. Both these possibilities give rise to two more possibilities depending on the other player. Since the second player plays optimally and try to minimise our score. So overall we have two possibilities at each instance.
For the first possibility , where we could pick the first element , the other player will pick the next element from the side that would minimize our total score.
Similarly , for the second possibility , where we can pick the last element , the other player would still pick the next element from the side that would minimize our total score.
We entertain both these cases and take the maximum result of the two and return that result.
We take two pointer variables , say ‘i’ and ‘j’ which each represent the starting and the ending point of the remaining array currently in consideration. We work till the two pointers cross each other

Okay i understood what it means that both will pick the coin optimally. But can you please tell me what is wrong in my second attempt? https://ide.codingblocks.com/s/287724

Thank you

You are actually follwing a greedy approach and only checking for maximum.
Remember that both players play optimally
Refer this https://ide.codingblocks.com/s/290620

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.