Consider a row of N coins of values V1 . . . Vn, where N is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely win if we move first.
Note: The opponent is as clever as the user.
Examples:
Input: {5, 3, 7, 10}
Output: 15 -> (10 + 5)
Input: {8, 15, 3, 7}
Output: 22 -> (7 + 15)
Why greedy algorithm fails here?
Does choosing the best at each move give an optimal solution? No.
In the second example, this is how the game can be finished in two ways:
- …….The user chooses 8.
…….The opponent chooses 15.
…….The user chooses 7.
…….The opponent chooses 3.
The total value collected by the user is 15(8 + 7)
- …….The user chooses 7.
…….The opponent chooses 8.
…….The user chooses 15.
…….The opponent chooses 3.
The total value collected by the user is 22(7 + 15)
Note: If the user follows the second game state, the maximum value can be collected although the first move is not the best.
Optimal Strategy for a Game using memoization:
To solve the problem follow the below idea:
There are two choices:
- The user chooses the ‘ith’ coin with value ‘Vi’: The opponent either chooses (i+1)th coin or jth coin. The opponent intends to choose the coin which leaves the user with minimum value.
i.e. The user can collect the value Vi + min(F(i+2, j), F(i+1, j-1) ) where [i+2,j] is the range of array indices available to the user if the opponent chooses Vi+1 and [i+1,j-1] is the range of array indexes available if opponent chooses the jth coin.

- The user chooses the ‘jth’ coin with value ‘Vj’: The opponent either chooses ‘ith’ coin or ‘(j-1)th’ coin. The opponent intends to choose the coin which leaves the user with the minimum value, i.e. the user can collect the value Vj + min(F(i+1, j-1), F(i, j-2) ) where [i,j-2] is the range of array indices available for the user if the opponent picks jth coin and [i+1,j-1] is the range of indices available to the user if the opponent picks up the ith coin.

Below is the recursive approach that is based on the above two choices. We take a maximum of two choices.
F(i, j) represents the maximum value the user
can collect from i’th coin to j’th coin.
F(i, j) = Max(Vi + min(F(i+2, j), F(i+1, j-1) ),
Vj + min(F(i+1, j-1), F(i, j-2) ))
As user wants to maximise the number of coins.
Base Cases
F(i, j) = Vi If j == i
F(i, j) = max(Vi, Vj) If j == i + 1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > arr;
map<vector< int >, int > memo;
int n = arr.size();
int solve( int i, int j)
{
if ((i > j) || (i >= n) || (j < 0))
return 0;
vector< int > k{ i, j };
if (memo[k] != 0)
return memo[k];
int option1
= arr[i]
+ min(solve(i + 2, j), solve(i + 1, j - 1));
int option2
= arr[j]
+ min(solve(i + 1, j - 1), solve(i, j - 2));
memo[k] = max(option1, option2);
return memo[k];
}
int optimalStrategyOfGame()
{
memo.clear();
return solve(0, n - 1);
}
int main()
{
arr.push_back(8);
arr.push_back(15);
arr.push_back(3);
arr.push_back(7);
n = arr.size();
cout << optimalStrategyOfGame() << endl;
arr.clear();
arr.push_back(2);
arr.push_back(2);
arr.push_back(2);
arr.push_back(2);
n = arr.size();
cout << optimalStrategyOfGame() << endl;
arr.clear();
arr.push_back(20);
arr.push_back(30);
arr.push_back(2);
arr.push_back(2);
arr.push_back(2);
arr.push_back(10);
n = arr.size();
cout << optimalStrategyOfGame() << endl;
}
|
Java
import java.util.*;
class GFG {
static ArrayList<Integer> arr = new ArrayList<>();
static HashMap<ArrayList<Integer>, Integer> memo
= new HashMap<>();
static int n = 0 ;
static int solve( int i, int j)
{
if ((i > j) || (i >= n) || (j < 0 ))
return 0 ;
ArrayList<Integer> k = new ArrayList<Integer>();
k.add(i);
k.add(j);
if (memo.containsKey(k))
return memo.get(k);
int option1 = arr.get(i)
+ Math.min(solve(i + 2 , j),
solve(i + 1 , j - 1 ));
int option2 = arr.get(j)
+ Math.min(solve(i + 1 , j - 1 ),
solve(i, j - 2 ));
memo.put(k, Math.max(option1, option2));
return memo.get(k);
}
static int optimalStrategyOfGame()
{
memo.clear();
return solve( 0 , n - 1 );
}
public static void main(String[] args)
{
arr.add( 8 );
arr.add( 15 );
arr.add( 3 );
arr.add( 7 );
n = arr.size();
System.out.println(optimalStrategyOfGame());
arr.clear();
arr.add( 2 );
arr.add( 2 );
arr.add( 2 );
arr.add( 2 );
n = arr.size();
System.out.println(optimalStrategyOfGame());
arr.clear();
arr.add( 20 );
arr.add( 30 );
arr.add( 2 );
arr.add( 2 );
arr.add( 2 );
arr.add( 10 );
n = arr.size();
System.out.println(optimalStrategyOfGame());
}
}
|
Python3
def optimalStrategyOfGame(arr, n):
memo = {}
def solve(i, j):
if i > j or i > = n or j < 0 :
return 0
k = (i, j)
if k in memo:
return memo[k]
option1 = arr[i] + min (solve(i + 2 , j), solve(i + 1 , j - 1 ))
option2 = arr[j] + min (solve(i + 1 , j - 1 ), solve(i, j - 2 ))
memo[k] = max (option1, option2)
return memo[k]
return solve( 0 , n - 1 )
arr1 = [ 8 , 15 , 3 , 7 ]
n = len (arr1)
print (optimalStrategyOfGame(arr1, n))
arr2 = [ 2 , 2 , 2 , 2 ]
n = len (arr2)
print (optimalStrategyOfGame(arr2, n))
arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ]
n = len (arr3)
print (optimalStrategyOfGame(arr3, n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static List< int > arr = new List< int >();
static Dictionary<List< int >, int > memo
= new Dictionary<List< int >, int >();
static int n = 0;
static int solve( int i, int j)
{
if ((i > j) || (i >= n) || (j < 0))
return 0;
List< int > k = new List< int >{ i, j };
if (memo.ContainsKey(k))
return memo[k];
int option1 = arr[i]
+ Math.Min(solve(i + 2, j),
solve(i + 1, j - 1));
int option2 = arr[j]
+ Math.Min(solve(i + 1, j - 1),
solve(i, j - 2));
memo[k] = Math.Max(option1, option2);
return memo[k];
}
static int optimalStrategyOfGame()
{
memo.Clear();
return solve(0, n - 1);
}
public static void Main( string [] args)
{
arr.Add(8);
arr.Add(15);
arr.Add(3);
arr.Add(7);
n = arr.Count;
Console.WriteLine(optimalStrategyOfGame());
arr.Clear();
arr.Add(2);
arr.Add(2);
arr.Add(2);
arr.Add(2);
n = arr.Count;
Console.WriteLine(optimalStrategyOfGame());
arr.Clear();
arr.Add(20);
arr.Add(30);
arr.Add(2);
arr.Add(2);
arr.Add(2);
arr.Add(10);
n = arr.Count;
Console.WriteLine(optimalStrategyOfGame());
}
}
|
Javascript
<script>
function optimalStrategyOfGame(arr, n)
{
let memo = {};
function solve(i, j)
{
if ( (i > j) || (i >= n) || (j < 0))
return 0;
let k = (i, j);
if (memo.hasOwnProperty(k))
return memo[k];
let option1 = arr[i] + Math.min(solve(i+2, j), solve(i+1, j-1));
let option2 = arr[j] + Math.min(solve(i+1, j-1), solve(i, j-2));
memo[k] = Math.max(option1, option2);
return memo[k];
}
return solve(0, n-1);
}
let arr1 = [ 8, 15, 3, 7 ];
let n = arr1.length;
console.log(optimalStrategyOfGame(arr1, n));
let arr2 = [ 2, 2, 2, 2 ];
n = arr2.length;
console.log(optimalStrategyOfGame(arr2, n));
let arr3 = [ 20, 30, 2, 2, 2, 10 ];
n = arr3.length;
console.log(optimalStrategyOfGame(arr3, n));
</script>
|
Time complexity: O(n^2), The time complexity of this approach is O(n^2) as we are using memoization to store the subproblem solutions which are calculated again and again.
Auxiliary Space: O(n^2), The space complexity of this approach is O(n^2) as we are using a map of size n^2 to store the solutions of the subproblems.
Optimal Strategy for a Game using dp:
To solve the problem follow the below idea:
Since the same subproblems are called again, this problem has the Overlapping Subproblems property. So the re-computations of the same subproblems can be avoided by constructing a temporary array in a bottom-up manner using the above recursive formula.
Follow the below steps to solve the problem:
- Create a 2-D array table of size N * N
- Run a nested for loop to consider i and j at every possible position with a distance equal to ‘gap’ between them
- Declare an integer x, If (i+2) is less than or equal to j then set x equal to table[i+2][j], else equal to zero
- Declare an integer y, If (i+1) is less than or equal to j-1 then set y equal to table[i+1][j-1], else equal to zero
- Declare an integer z, If i is less than or equal to j-2 then set z equal to table[i][j-2], else equal to zero
- Set table[i][j] equal to maximum of arr[i] + min(x, y) or arr[j] + min(y, z)
- Return table[0][N-1]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int optimalStrategyOfGame( int * arr, int n)
{
int table[n][n];
for ( int gap = 0; gap < n; ++gap) {
for ( int i = 0, j = gap; j < n; ++i, ++j) {
int x = ((i + 2) <= j) ? table[i + 2][j] : 0;
int y = ((i + 1) <= (j - 1))
? table[i + 1][j - 1]
: 0;
int z = (i <= (j - 2)) ? table[i][j - 2] : 0;
table[i][j] = max(arr[i] + min(x, y),
arr[j] + min(y, z));
}
}
return table[0][n - 1];
}
int main()
{
int arr1[] = { 8, 15, 3, 7 };
int n = sizeof (arr1) / sizeof (arr1[0]);
printf ( "%d\n" , optimalStrategyOfGame(arr1, n));
int arr2[] = { 2, 2, 2, 2 };
n = sizeof (arr2) / sizeof (arr2[0]);
printf ( "%d\n" , optimalStrategyOfGame(arr2, n));
int arr3[] = { 20, 30, 2, 2, 2, 10 };
n = sizeof (arr3) / sizeof (arr3[0]);
printf ( "%d\n" , optimalStrategyOfGame(arr3, n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int optimalStrategyOfGame( int arr[], int n)
{
int table[][] = new int [n][n];
int gap, i, j, x, y, z;
for (gap = 0 ; gap < n; ++gap) {
for (i = 0 , j = gap; j < n; ++i, ++j) {
x = ((i + 2 ) <= j) ? table[i + 2 ][j] : 0 ;
y = ((i + 1 ) <= (j - 1 ))
? table[i + 1 ][j - 1 ]
: 0 ;
z = (i <= (j - 2 )) ? table[i][j - 2 ] : 0 ;
table[i][j]
= Math.max(arr[i] + Math.min(x, y),
arr[j] + Math.min(y, z));
}
}
return table[ 0 ][n - 1 ];
}
public static void main(String[] args)
{
int arr1[] = { 8 , 15 , 3 , 7 };
int n = arr1.length;
System.out.println(
"" + optimalStrategyOfGame(arr1, n));
int arr2[] = { 2 , 2 , 2 , 2 };
n = arr2.length;
System.out.println(
"" + optimalStrategyOfGame(arr2, n));
int arr3[] = { 20 , 30 , 2 , 2 , 2 , 10 };
n = arr3.length;
System.out.println(
"" + optimalStrategyOfGame(arr3, n));
}
}
|
Python3
def optimalStrategyOfGame(arr, n):
table = [[ 0 for i in range (n)]
for i in range (n)]
for gap in range (n):
for j in range (gap, n):
i = j - gap
x = 0
if ((i + 2 ) < = j):
x = table[i + 2 ][j]
y = 0
if ((i + 1 ) < = (j - 1 )):
y = table[i + 1 ][j - 1 ]
z = 0
if (i < = (j - 2 )):
z = table[i][j - 2 ]
table[i][j] = max (arr[i] + min (x, y),
arr[j] + min (y, z))
return table[ 0 ][n - 1 ]
arr1 = [ 8 , 15 , 3 , 7 ]
n = len (arr1)
print (optimalStrategyOfGame(arr1, n))
arr2 = [ 2 , 2 , 2 , 2 ]
n = len (arr2)
print (optimalStrategyOfGame(arr2, n))
arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ]
n = len (arr3)
print (optimalStrategyOfGame(arr3, n))
|
C#
using System;
public class GFG {
static int optimalStrategyOfGame( int [] arr, int n)
{
int [, ] table = new int [n, n];
int gap, i, j, x, y, z;
for (gap = 0; gap < n; ++gap) {
for (i = 0, j = gap; j < n; ++i, ++j) {
x = ((i + 2) <= j) ? table[i + 2, j] : 0;
y = ((i + 1) <= (j - 1))
? table[i + 1, j - 1]
: 0;
z = (i <= (j - 2)) ? table[i, j - 2] : 0;
table[i, j]
= Math.Max(arr[i] + Math.Min(x, y),
arr[j] + Math.Min(y, z));
}
}
return table[0, n - 1];
}
static public void Main()
{
int [] arr1 = { 8, 15, 3, 7 };
int n = arr1.Length;
Console.WriteLine( ""
+ optimalStrategyOfGame(arr1, n));
int [] arr2 = { 2, 2, 2, 2 };
n = arr2.Length;
Console.WriteLine( ""
+ optimalStrategyOfGame(arr2, n));
int [] arr3 = { 20, 30, 2, 2, 2, 10 };
n = arr3.Length;
Console.WriteLine( ""
+ optimalStrategyOfGame(arr3, n));
}
}
|
PHP
<?php
function optimalStrategyOfGame( $arr , $n )
{
$table = array_fill (0, $n ,
array_fill (0, $n , 0));
for ( $gap = 0; $gap < $n ; ++ $gap )
{
for ( $i = 0, $j = $gap ; $j < $n ; ++ $i , ++ $j )
{
$x = (( $i + 2) <= $j ) ?
$table [ $i + 2][ $j ] : 0;
$y = (( $i + 1) <= ( $j - 1)) ?
$table [ $i + 1][ $j - 1] : 0;
$z = ( $i <= ( $j - 2)) ?
$table [ $i ][ $j - 2] : 0;
$table [ $i ][ $j ] = max( $arr [ $i ] + min( $x , $y ),
$arr [ $j ] + min( $y , $z ));
}
}
return $table [0][ $n - 1];
}
$arr1 = array ( 8, 15, 3, 7 );
$n = count ( $arr1 );
print (optimalStrategyOfGame( $arr1 , $n ) . "\n" );
$arr2 = array ( 2, 2, 2, 2 );
$n = count ( $arr2 );
print (optimalStrategyOfGame( $arr2 , $n ) . "\n" );
$arr3 = array (20, 30, 2, 2, 2, 10);
$n = count ( $arr3 );
print (optimalStrategyOfGame( $arr3 , $n ) . "\n" );
?>
|
Javascript
<script>
function optimalStrategyOfGame(arr, n)
{
let table = new Array(n);
let gap, i, j, x, y, z;
for (let d = 0; d < n; d++)
{
table[d] = new Array(n);
}
for (gap = 0; gap < n; ++gap)
{
for (i = 0, j = gap; j < n; ++i, ++j)
{
x = ((i + 2) <= j) ? table[i + 2][j] : 0;
y = ((i + 1) <= (j - 1)) ?
table[i + 1][j - 1] : 0;
z = (i <= (j - 2)) ? table[i][j - 2] : 0;
table[i][j] = Math.max(
arr[i] + Math.min(x, y),
arr[j] + Math.min(y, z));
}
}
return table[0][n - 1];
}
let arr1 = [ 8, 15, 3, 7 ];
let n = arr1.length;
document.write( "" + optimalStrategyOfGame(arr1, n) +
"</br>" );
let arr2 = [ 2, 2, 2, 2 ];
n = arr2.length;
document.write( "" + optimalStrategyOfGame(arr2, n) +
"</br>" );
let arr3 = [ 20, 30, 2, 2, 2, 10 ];
n = arr3.length;
document.write( "" + optimalStrategyOfGame(arr3, n));
</script>
|
Time Complexity: O(N2).
Auxiliary Space: O(N2). As a 2-D table is used for storing states.
Note: The above solution can be optimized by using less number of comparisons for every choice. Please refer below.
Optimal Strategy for a Game | Set 2
Exercise:
Your thoughts on the strategy when the user wishes to only win instead of winning with the maximum value. Like the above problem, the number of coins is even.
Can the Greedy approach work quite well and give an optimal solution? Will your answer change if the number of coins is odd? Please see Coin game of two corners
This article is compiled by Aashish Barnwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Sep, 2023
Like Article
Save Article