have i done right?
Optimal Game Strategy
@i_am_dekard_shaw
your recurrence relation (say f(arr,i,j)) for this problem should be something like
if piyush turn then
return max(arr[i]+f(arr,i+1,j),arr[j]+f(arr,i,j-1))
if nimit turn then
return min(f(arr,i+1,j),f(arr,i,j-1))
@i_am_dekard_shaw
for the test i mentioned above your code will never hit the base case( and u will get tle because of infinite recursion).
if u will dry run on the above test case you will find ur mistake.
@i_am_dekard_shaw
everthing in ur code is correct .only u missed one base case. u should handle case when their is only two elements in your array .in this case u should return maximum among two.
something like

btw your recurrence relation is impressive😎
1 Like