The way i understood the question, any player can choose any element from the first or the last without any restriction. If piyush moves first and he takes last element that is 4 and say nimit takes first element that is 1 and again piyush takes last element from the remaining array that is 3 so the sum becomes 7 but we have to output 6. Can anyone explain this. The code which i have written gives me output 7. https://ide.codingblocks.com/s/88365
Wrong output, or wrong test cases
Hi @mudit2jain,
see if piyush gets the first turn so he will select 4 as it is greater than 1 now in nimish turn he will select 3 as 3>1 now piyush will take 2 as 2>1 and nimish will take 1 so total of piyush will become 6.Both the players will choose the numbers such that their count becomes maximum.
In the question itself it is not written anywhere that number will be picked up because it is greater than 1. It is clearly written that they either choose from first or last and then remove that number from the row.
It is mentioned in question that both the players will play optimally. So, at any point of time both will try to maximize the coins earned.
Initial Array : {1,2,3,4}
Piyush Turn : He will think of taking maximum of first and last coin, so he will select coin 4.
New Array : {1,2,3}
Nimit Turn : He will play optimally and will think of taking Maximum of first and last coin which is 3.
New Array : {1,2}
Piyush Turn : He will select maximum of 1 and 2, which is 2. So, coins earned by piyush is 4+2 = 6.
ok,
I tried and it is not working for 1 test case it is not working
actually the language of the ques is a bit confusing…but after seeing the tc i think what you actually have to do is that at each move a player will have 2 option either pick the coin at starting or at ending…he will chose that coin which will give him OVERALL max sum…both the players will want to go for a win
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.
I have understood completely about the question and i have made my approach after that. Can you please look at that code and help me in solving the testcases.
@mudit2jain
Consider
4
8 15 3 7
In this e.g. Piyush can make upto 22 coins,as he need to maximize the number of coins.
Explanation: Piyush takes 7 first,Namit is left with 8 15 3 Namit take either 8,3 it will not effect Piyush’s max coins as in next turn in both cases:
i)Namit takes 8,Piyush is left with either 15 3 he will take 15,hence his coins are 7+15=22.
ii)Namit takes 3,Piyush is left with either 8 15 he will take 15,hence his coins are 7+15=22.
Thankyou,it will certainly help.