Two Ends

In the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. The player whose cards add up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest — we’ll call this the greedy strategy. However, this is not always optimal, as the following example shows: (The first player would win if she would first pick the 3 instead of the 4.)

3 2 10 4

You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes.

Input
There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.

Output
For each test case you should print one line of output of the form:

In game m, the greedy strategy might lose by as many as p points.

where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player’s score and second player’s score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.

Example
Input:
4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0

Output:
In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 points.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication2
{
    class Program
    {
        public static int maxDiff = 0;
        public static bool flag = true;
        static void Main(string[] args)
        {
            int[] numArray = {2, 2, 1, 5, 3, 8, 7, 3};
            Play(numArray, true, 0, numArray.Length - 1, 0, 0);
            Console.WriteLine("Max diff possible = " + maxDiff);
        }
        public static void Play(int[] numArray, bool first, int start, int end, int firstSum, int secondSum)
        {
            //Check for termination
            if (start > end)
            {
                if (flag == true)
                {
                    flag = false;
                    Console.WriteLine("First Sum = " + firstSum);
                    Console.WriteLine("Second Sum = " + secondSum);
                    Console.WriteLine("===============");
                    maxDiff = firstSum - secondSum;
                }
                else
                {
                    if (maxDiff < (firstSum - secondSum))
                    {
                        maxDiff = firstSum - secondSum;
                    }
                    Console.WriteLine("First Sum = " + firstSum);
                    Console.WriteLine("Second Sum = " + secondSum);
                    Console.WriteLine("===============");
                }
                return;
            }
            //Check for turn
            if (first)
            {
                if (start != end)
                {
                    //Remove from left
                    Play(numArray, false, start + 1, end, firstSum + numArray[start], secondSum);

                    //Remove from right
                    Play(numArray, false, start, end - 1, firstSum + numArray[end], secondSum);
                }
                else
                {
                    //Remove from left
                    Play(numArray, false, start + 1, end, firstSum + numArray[start], secondSum);
                }
            }
            else
            {
                if (numArray[start] >= numArray[end])
                {
                    Play(numArray, true, start + 1, end, firstSum, secondSum + numArray[start]);
                }
                else
                {
                    Play(numArray, true, start, end - 1, firstSum, secondSum + numArray[end]);
                }
            }
        }
    }
}

Output
=======
First Sum = 10
Second Sum = 21
===============
First Sum = 8
Second Sum = 23
===============
First Sum = 13
Second Sum = 18
===============
First Sum = 17
Second Sum = 14
===============
First Sum = 12
Second Sum = 19
===============
First Sum = 16
Second Sum = 15
===============
First Sum = 14
Second Sum = 17
===============
First Sum = 13
Second Sum = 18
===============
First Sum = 8
Second Sum = 23
===============
First Sum = 12
Second Sum = 19
===============
First Sum = 10
Second Sum = 21
===============
First Sum = 9
Second Sum = 22
===============
First Sum = 15
Second Sum = 16
===============
First Sum = 14
Second Sum = 17
===============
First Sum = 18
Second Sum = 13
===============
First Sum = 17
Second Sum = 14
===============
Max diff possible = 5
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s