Question Doubt!

4
1 2 3 4

Doesn’t the output of this should be 7 because at first piyush picks 4 then nimit picks 1 and then piyush can pick 3 right?

hello @haseebshaik00
nimit also wants to maximize his score right? becuase both of them are playing optimally (i.e they both want to maximize their own score).
thats why when piyush will pick 4 then nimit will pick 3 and then piyush will pick 2 and then nimit will pick 1.

hence the score of piyush in games ending will be 4 +2 =6

oh thank you!!!

Cant pass 2 cases : https://ide.codingblocks.com/s/352577

@haseebshaik00

it is partially correct.

here is the approach->

we would need an optimal solution for this. At each instance we would need to consider two possibilities that we can pick the first as well as the last element of the remaining array. Both these possibilities give rise to two more possibilities depending on the other player. Since the second player plays optimally and try to minimise our score. So overall we have two possibilities at each instance.
For the first possibility , where we could pick the first element , the other player will pick the next element from the side that would minimise our total score.
Similarly , for the second possibility , where we can pick the last element , the other player would still pick the next element from the side that would minimise our total score.
We entertain both these cases and take the maximum result of the two and return that result.
We take two pointer variables , say ‘i’ and ‘j’ which each represent the starting and the ending point of the remaining array currently in consideration. We work till the two pointers cross each other.

code for reference->

// Consider both the possibilities. You can pick either the first or the last coin.
// Since the opponent plays optimally , we would get the minimum of the remaining coins for each choice.

ll pickFirst = coins[i] + min( optimalGame(i+2,j) , optimalGame(i+1,j-1) ) ;

ll pickLast = coins[j] + min( optimalGame(i,j-2) , optimalGame(i+1,j-1) ) ;

I didn’t understand this one

hello @haseebshaik00 what you have not understood .
you have not understood the question properly or these 2 lines of code ?

the 2 lines of code, why are we taking the minimum ?

@haseebshaik00 we are taking the minimum because in the question it is given that both the players play optimally and we have to make piyush win .
thats why nimit also play optimally he want him to win so he will also pick the coin with greater value so that he can win and piyush can lose .
that’s why we are minimising the nimit’s case and we are maximising the piyush’s case .
if you have seen the correct code then there after these two lines there must be the line in which we are finding the maximum of the both so that it can be credited on piyush’s name . we have to minimise the piyush’s total thats why we are taking the minimum .
i hope this clears your doubt .
if you still have any doubt please ask here .
if your doubt is cleared please mark this doubt as resolved .
Happy Learning !!