[Math] Alice and Bob number sum game

combinatoricsgame theorynumber theory

Alice and Bob play a game with first $N$ positive numbers. Out of these $N$ integers some $K$ integers are missing. So both decided to play with remaining $N-K$ integers and in this game Alice wants Bob to win the game.

Game is as follow :

In $X$th move, a player can choose some numbers out of all the numbers available such that chosen numbers sum upto $X$. After the move, chosen numbers are placed back on the table. The player who is not able to make a move loses.

Given that both players play optimally and Alice decides who should go first we need to find who should take the first turn so that Bob wins the game.

Example : Let $N=5$ and $K=2$ and let numbers that are missing are : $\{3,5\}$ so the left out numbers are : $\{1,2,4\}$. Now if Bob plays first then there is always a chance for Bob to win.

Explanation :

Bob can choose {1} to make 1.
Alice can choose {2} to make 2.
Bob can choose {1,2} to make 3.
Alice can choose {4} to make 4.
Bob can choose {1,4} to make 5.
Alice can choose {2,4} to make 6.
Bob can choose {1,2,4} to make 7.
Alice cannot make 8 out of the numbers on the table.
So, Alice loses and Bob wins.

Best Answer

There is really only one "move" in the game. Alice needs to decide who should go first. After that, both players are playing mechanically.

To decide who goes first, Alice finds the least X such that no subset of the available numbers adds up to X. Play will end on turn X, and Alice wants to be the one whose turn that is. So, if X is odd, Alice plays first; otherwise she lets Bob play first.

To easily determine X, Alice uses a Sieve. You're probably familiar with the Sieve of Eratosthenes, used to find primes. Same idea, but with different rules for a different purpose.

Alice writes down the numbers from 0 to the sum of all the numbers on the table, and crosses off 0. Then she takes numbers from the table, one at a time, without replacement. When she takes off the number Y, she crosses off every number that is Y plus some previously-crossed off number. When she has done this for every number on the table, the smallest un-crossed-off number is X.

Notice that this works even if the numbers on the table are not all distinct.

It even works if some of them are negative or zero, except that then Alice's initial list runs from the sum of the negative numbers thru the sum of the positive numbers, and X is the least positive un-crossed-off number.


Improved solution

Assume the numbers on the table are distinct positive integers.

If there were no missing numbers, any sum up to $S(N) = N(N+1)/2$ could easily be gotten, and X would be $S(N) = 1$.

But there are missing numbers. Let's start with an empty table, add numbers to it in increasing order, and after placing each number $n$ compute $S(n)$ = sum of all numbers on the table up to $n$. The question is, is it still true that a player could get any sum up to $S(n)$ from these numbers, assuming it was true for $S(n-1)$ before placing $n$.

Well, if $n \le S(n-1)+1$, then the answer is clearly yes. You could already get any sum up to $S(n-1)$ without using $n$. To get a sum $s$ where $S(n-1) \lt s \le S(n)$, combine $n$ with a combination summing to $s - n$ from the smaller numbers.

Conversely, if $n \gt S(n-1)+1$, then the answer is no. The sum $S(n-1)+1$ is unachievable without using $n$, because it exceeds the sum of the other numbers, and is also unachievable using $n$, because $n$ by itself is already too large.

And that leads to an obvious $O(N)$ algorithm to find X

X = 1
for n from 1 to N
    break if n > X
    X += n unless n is one of the missing numbers
end
return X

(I assume it's obvious how to do unless n is one of the missing numbers in constant time, assuming they are given to us in ascending order.)

But if $K$ is much smaller than $N$ we can do better. Suppose we're given the missing numbers in ascending order. Then

MSUM = 0
for each missing number m
    MSUM += m
    X = m * (m + 1)/2 - MSUM + 1
    return X if m >= X
end
return N * (N + 1)/2 - MSUM + 1

This runs in time $O(K)$.