Why is this code not working, Please help for this one?

Why is this code not working, Please help for this one?


Also check the Recursive Approach I wrote below.

hello @cbcao263

u r restricted to pick only left most or right most indices that means given order of array matters hence u cannot sort the array.

also it is mentioned that both player are playing optimally that means greedy wont work here.

ur recusive approach is in correct direction, try to think a bit more on it


This is what I implemented, Getting error in returning.

no still u r trying a greedy approach.

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->

Sir, Is this Question of Game Theory(I don’t know about this but have heard) or of DP like 2D DP as I and j will be changing here.

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.