Regarding the error

Hey @gargshivam2001 i am telling you the test case in which you are getting wrong answer
Input:
10
22 50 16 13 8 41 25 43 8 7
Expected output is: 154
Yours is giving: 79
Also use long long int as value of Ai is high and make your array A of size n not of 100 cause value of N is bigger then 100.

sir,
could you please tell the logic of the ques for the input :
10
22 50 16 13 8 41 25 43 8 7
how the output is 154 ???

please reply sir !!!

I am framing an explanation For you please wait.

So to tell you how this problem works is let me explain it to you with an example
Suppose in my array I have
100 1000 10 90

Here I will pick 90 first so that opponent don’t get access to 1000 and he will pick 100 then I pick 1000
and he picks 10 so answer is 1090
There are two choices:

  1. The user chooses the ‘ith’ coin with value ‘Vi’: The opponent either chooses (i+1)th coin or jth coin. The opponent intends to choose the coin which leaves the user with minimum value .
    i.e. The user can collect the value Vi + min(F(i+2, j), F(i+1, j-1) ) .
    coinGame1
  2. The user chooses the ‘jth’ coin with value ‘Vj’: The opponent either chooses ‘ith’ coin or ‘(j-1)th’ coin. The opponent intends to choose the coin which leaves the user with minimum value, i.e. the user can collect the value Vj + min(F(i+1, j-1), F(i, j-2) ) .
    coinGame2

We are sending minimum of i+2,j and i+1,j-1 cause both players are planing to play optimally So both are ready to cut each other points by sending minimum value to them and reserving maximum value for themselves.

Opponent will make sure that i don’t get maximum from these two position that is i+2,j,i+1 & j-1

Whereas

I will make sure that he didn’t get maximum from these two positions that is i+1,j-1,i & j-2
Now if you have any other doubts left feel free to ask.