Optimal game stratergy 1

sir i coded the soln for this problem but im not getting all test cases passed. i got 25/100 and a message was displayed -

Dang! You couldn’t score a perfect 100 because you failed one or more testcases. This means that your program didn’t account for all input cases (did you account for negative numbers, for example?).

please somebody have a look into my code and tell me what am i doing wrong???

my code is -

Hey @pushkar24sharma
Your logic is incorrect
Always picking the largest element not always work
say we have

4
100 1000 10 90

Here if we pick 100 then opponent picks 1000
So its better to pick 90 and later 1000

logic:
Remember both A and B are equally smart and we want A to get maximum.
if coins are indexed from i to j and both have to choose one out of two ends.

case 1: if A chooses a[i], now opponent can choose either a[i+1] or a[j] whatever is maximum out of two. which means A will get Minimum in his next chance based on opponent choice.
case 1.a: if opponent choose a[i+1], player will chose either a[i+2] or or a[j]
case 1.b: if opponent choose a[j], player will chose either a[i+1] or or a[j-1]
So A will get minimum out of case 1.a and case 1.b…as opponent is equally smarter and choose such a coin which give him maximum but minimum to player A.

case 2: if A chooses a[j], now opponent can choose either a[i] or a[j-1] whatever is maximum out of two. which means A will get Minimum in his next chance based on opponent choice.
case 2.a: if opponent choose a[i], player will chose either a[i+1] or or a[j-1]
case 2.b: if opponent choose a[j-1], player will chose either a[i] or or a[j-2]
So A will get minimum out of case 2.a and case 2.a…as opponent is equally smarter and choose such a coin which give him maximum but minimum to player A.

This will give you a recursive case in which final answer is max(case1,case2)

Here check this for reference

1 Like

@Kartikkhariwal1 ok thank u sir i got where i was doing wrong and have understood the real soln

1 Like

If u don’t have any other doubt in this then please mark it as resolved :slight_smile:

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.