2 Player and N Coin – Strategy Puzzle

Puzzle:

There are n coins in a line. (Assume n is even). Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.

Would you rather go first or second? Does it matter?
Assume that you go first, describe an algorithm to compute the maximum amount of money you can win.

Note that the strategy to pick maximum of two corners may not work. In the following example, first player looses the game when he/she uses strategy to pick maximum of two corners.

Example 18 20 15 30 10 14
First Player picks 18, now row of coins is
  20 15 30 10 14
Second player picks 20, now row of coins is
  15 30 10 14
First Player picks 15, now row of coins is
  30 10 14
Second player picks 30, now row of coins is
  10 14
First Player picks 14, now row of coins is
  10 
Second player picks 10, game over.

The total value collected by second player is more (20 + 30 + 10) compared to first player (18 + 15 + 14). So the second player wins.

Puzzle Solution:

Going first will guarantee that you will not lose. By following the strategy below, you will always win the game (or get a possible tie).

(1) Count the sum of all coins that are odd-numbered. (Call this X)
(2) Count the sum of all coins that are even-numbered. (Call this Y)
(3) If X > Y, take the left-most coin first. Choose all odd-numbered coins in subsequent moves.
(4) If X < Y, take the right-most coin first. Choose all even-numbered coins in subsequent moves.
(5) If X == Y, you will guarantee to get a tie if you stick with taking only even-numbered/odd-numbered coins.

You might be wondering how you can always choose odd-numbered/even-numbered coins. Let me illustrate this using an example where you have 6 coins:

Example
18 20 15 30 10 14
Sum of odd coins = 18 + 15 + 10 = 43
Sum of even coins = 20 + 30 + 14 = 64.
Since the sum of even coins is more, the first player decides to collect all even coins. He first picks 14, now the other player can only pick a coin (10 or 18). Whichever is picked the other player, the first player again gets an opportunity to pick an even coin and block all even coins.

4 Thoughts on “2 Player and N Coin – Strategy Puzzle

  1. prasad on August 8, 2016 at 1:05 pm said:

    coins = (18,20,15,30,10,14)
    listSize = len(coins)
    even = []
    odd = []
    p1 = []
    p2 = []
    add1 = 0
    add2 = 0
    for index in range(0,listSize):
    if int(coins[index])%2 == 0:
    even.append(coins[index])
    else:
    odd.append(coins[index])
    if len(even) > len(odd):
    for index in range(0,listSize):
    if coins[index]%2 == 0:
    p1.append(coins[index])
    else:
    p2.append(coins[index])
    for index in range(0,len(p1)):
    add1 = add1+p1[index]
    for index in range(0,len(p2)):
    add2 = add2+p2[index]

    print add1
    print add2

  2. for Input = {9,18,2}

    X = 11 (9 + 2)
    Y = 18 (18)

    as X < Y He picks 2
    the second player picks 18

    In this scenario going first wont work.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation