one test case showing wrong answer
Optimal game strategy recursion
Hello @sam15jain ,
This is happening because of the confusion that is being created due to a condition:
4
1 4 3 1
if(coins[s]>=coins[e]){
if(turnNo&1){
sum+=coins[s];
}
int a = optimalstrategy(coins,s+1,e,sum,turnNo+1,n);
}
Your code will always choose the s irrespective of the next unrevealed element, if coin at s and e are same.
Hope, this would help.
Let me know if you still face any doubt.
what should i do instead
You can check for compare elements coins[s+1] and coins[e-1]
Like in the above example:
when coins[s]=coins[e]=1
compare, 3 and 4.
if coins[e-1] is greater, assign coins[s]. This way you will gey a chance to choose bigger number(i.e. 4)
Hope, this would help.