my one test case is not running please help me out
Optimal game strategy -1
Please send the link of your code
There is no need to create two separate functions to solve this problem. I will explain a simpler approach.
Coins can be picked from either end. So we will have two values - one when coin is picked from the start and the other when coin is picked from the end. And we will return the maximum of the two values.
Let the two values be a and b, v be our array and s and e be the starting and ending indices respectively (s = 0 and e = n-1 initially).
maxvalue is the function name.
a = v[s] + min( maxvalue(v,s+2,e), maxvalue(v,s+1,e-1) );
b = v[e] + min( maxvalue(v,s+1,e-1), maxvalue(v,s,e-2) );
a is the value when the first player picks the coin from the start. So v[s] is added. Our array is from s+1 to e now. If the second player picks from the start, our search space becomes s+2 to e now and the first player will be left with maxvalue(v,s+2,e). And if he picks from the end, our search space becomes s+1 to e-1 and the first player will be left with maxvalue(v,s+1,e-1). Both players play optimally, so the second player will leave the minimum of the two values for the first player. Hence the first recurrence relation for a.
We can form the second recurrence relation for b similarly.
And finally we will return max(a,b).
can you tell the problem in my code
Your code is wrong because you are only picking coins from the front. Plus the value that you are displaying is the maximum of the value acquired by both the players which is totally wrong.
I suggest you to read and understand my approach as that is how this question should be solved. Try to code it and tell me if you have any problem.
26 2396 25316 30085 23080 10269 5711 8306 11536 19092 6816 6305 23649 32583 5585 14193 14859 30265 18026 5528 16126 15212 25591 14789 3900 31395 25529
can you explain me this test case and how the answer is 224098
This question is an optimization problem and there can be many possibilities of how the two players play the game. Its not a simple greedy approach. So rather than focusing on specific test cases, follow the approach which I have told you.