i used this approach
Approach to the problem
hello @tusharbhardwaj127 in this question as it is clearly mentioned that both the the players optimically i.e both will try to maximise their result .
now but you have to made piyush win so in order to do that you have to minimise the result of nimit .
i don’t think your approach is right .
here for your reference i am giving you the correct code .
please write here if you have query in anything .
Happy Learning !!
can you please explain the approach.
hello @tusharbhardwaj127 yes ,sure .
actually int this question it is mentioned clearly that both the players play optimally which means that both the players make moves so that they can win .
that why we have to minimise the another person number and we have to maximise the piyush number .
i have made two recursion call which are minimising the nimits case as piyush moves first and then we are returning or stroring the maximum of both as of piyush moves .
please read this explanation and go through the code again .
If you still have query please ask here.
i hope i have cleared your doubt .
Happy Learning !!
I am unable to get the idea behind. In the opt1 case after we choose the element at the i’th index the available index becomes(i+1, j) but why are we choosing from (i+2 ,j) and similarly in opt2 after choosing the jth the available elements becomes(i,j-1) but we are choosing from (i,j-2)…why??
@tusharbhardwaj127 we are doing this because:
int opt1=a[i]+ min(optimalgamestrategy(a,i+2,j),optimalgamestrategy(a,i+1,j-1));
int opt2=a[j]+ min(optimalgamestrategy(a,i,j-2),optimalgamestrategy(a,i+1,j-1));
these are the both conditions which we are doing .
in opt1 we have addded a value from the first so the second player have two options either to choose from the first or from the second .so as we have already added the (i+1)th value thats why the array left for the second player is from i+2 to j and if he chooses from the end then the array left is i+1 to j-1.
try to visualise the same for opt2 .
Tell me if you still have any error.
I hope i have cleared your doubt .
Happy Learning !!