Please help me to understand the question.
I could not understand the problem
Hi @raj.verma5454,
In dis question, u need to maximise the earning of Piyush.
in the given test case,
Input array={1,2,3,4}
Output=6
Here piyush first chooses 4 and 2
and Namit chooses 3 and 1.
Both players try to maximise there earning.
Consider another case:
Input={1,3,5,2}
Output=6
Here in first move, Piyush chooses 1.
Then Namit Chooses 3.
Then Piyush chooses 5.
Then Namit chooses 2.
As u can see, we cant follow greedy approach as if we had followed that Piyush would have chosen 2 and 3
and Namit would have chosen 5 and 1.
Hope this resolves ur doubt.
i got the question but couldn’t able to think the approach?
@raj.verma5454, this question is a dp problem. So, first u need to think about the recurrence relation.
Since piyush can pick a number from either end, u need to choose ans=max(piyush pick from left, piyush pick from right).
So, int x=arr[end]+min(namit pick from left from remaining array,namit pick from right from remaining array).
,int y=arr[begin]+min(namit pick from left remaining array,namit pick from right remaining array).
ans=max(x,y)
So, using this approach, u can right a top down dp.
If u still have any queries, u can post it 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.