1 test case not passing
The logic you applied was greedy, i.e. you always take maximum of first and last position and select it. It is not correct as in example
8 15 3 7
Your logic applies as : piyush -> 8, nimit -> 15, piyush -> 7, nimit -> 3
resulting in 8+7=15
but soln can be: piyush -> 7, nimit -> 8, piyush -> 15, nimit -> 3
resulting in 7+15=22
You need to think in terms of game playing algorithms. The opponents always chooses that position that will result in minimum score of the player.
Think a recursive solution for this.
Please tell if doubt is resolved.
Ok. Thanks for the discussion.