Piyush will pick the coin 4. Then Nimit can pick either 1 or 3. In both the cases piyush picks coin 2 and wins with a total of 6. But i want to ask if nimit picks coin 1 so the remaining coins will be 2 ,3 so piyush can pick coin 3 and have a total of 7 . Are we removing both the coins that nimit can choose from the array ?
I have build a program according to the logic of removing both :
#include
using namespace std;
int optimal(int *a, int n, int s, int e, int piyushTurn, int maxPiyush)
{
if (s > e)
{
return maxPiyush;
}
// piyush turn
if (piyushTurn % 2 == 1)
{
if (a[s] <= a[e])
{
maxPiyush += a[e];
return optimal(a, n, s , e - 1, piyushTurn + 1, maxPiyush);
}
else
{
maxPiyush += a[s];
return optimal(a, n, s + 1, e, piyushTurn + 1, maxPiyush);
}
}
// Nimit turn
else
{
return optimal(a, n, s + 1, e - 1, piyushTurn + 1, maxPiyush);
}
}
int main()
{
int n;
cin >> n;
int a[30];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << optimal(a, n, 0, n - 1, 1, 0);
return 0;
}
Please tell me where i am wrong