11
$\begingroup$

Recently, I've been given an interesting type of problem that I am having a difficult time proving. The problem revolves around a game, with the rules as follows:

"You have a deck containing n cards in random order. The top card has some number k on it. Take the top k cards off of the top of the deck, reverse their order, and put them back on top of the deck. A new number, r, is now at the top of the deck. Take r cards off the top of the deck, reverse their order, and put them back on top of the deck. Repeat this process until the game ends (Card #1 is at the top of the deck)."

The supposed outcome of the game is that it will take no more than F(n+1) - 1 "steps" to end the game. I have tried this experimentally, and have been able to verify the result for various values of n, but how to prove it is another story.

  • 0
    When you say "Card #1", do you mean the card with 1 on it or the card which started on top? Presumably the former as the latter may never terminate. I also assume the deck has numbers 1, 2, 3, ... $n$, though it might be worth saying so explicitly.2011-04-16
  • 0
    "Card #1" = The card with "1" on it. Your assumption is correct regarding 1, 2, 3, ... n cards being in the deck.2011-04-16

3 Answers 3

6

You can prove the game terminates by induction. A $1$ card deck terminates immediately. Now assume we have proven that it terminates for all decks up to $n$. If we imagine playing a game with $n+1$ cards, if card $n+1$ comes to the top of the deck, it will go to be bottom and can never enter play again. We are then playing an $n$ card game, which we know terminates. If card $n+1$ never comes to the top of the deck, we will never see the card on the bottom of the deck, so we can pretend it isn't there, in which case we are again playing an $n$ card game. So the game always terminates.

Added: I believe that an upper bound for the maximum number of turns is one less than the Fibonacci numbers, $F(n)$ with the index offset by $1$. Let $G(n)$ be the maximum number of turns for $n$ cards. $G(1)=0$ as card $1$ is on top. $G(2)=1$ for order $2,1$. $G(3)=2$ for order $3,1,2$ To find the recurrence, you don't want $1$ on the bottom or you will get it as soon as you find card $n$. So you are playing an $n-2$ card game which ends when you find card $n$ and haven't found card $1$. You play $1$ turn to put card $n$ on the bottom, then play an $n-1$ card game. So the recurrence is $G(n)=1+G(n-1)+G(n-2)$ To prove it by induction, note $G(1)=F(2)-1, G(2)=F(3)-1$ and assume we have proved it up to $n-1$, then $ G(n)=1+G(n-1)+G(n-2)=1+(F(n)-1)+(F(n-1)-1)=F(n+1)-1$

However, this assumes that you can play the $n-1$ card game avoiding $1$ for as many turns as an $n-2$ card game. For $4$ cards this works with the order $2413$ taking $4$ moves, and for $5$ it also works with $31452$ taking $7$. But for $6$ cards it fails. The way to find these is take the maximum series for $n-1$, put card $n$ on the bottom and work backwards. For $6$ you get stuck and $456213$ takes only $10$ turns. I haven't proven that there are no $11$ move decks.

  • 0
    Interesting point. It definitely terminates though... and concentrating on the highest numbered card seems to be a focal point. One thing I've noticed is that any card at the bottom of the pile can be unearthed only by having the high card at the top of the deck. So basically, the worst case scenario is that the "1" is at the bottom of the deck and you have to reveal the high card.2011-04-16
  • 0
    Well, the reason I put "1" at the bottom is that the only way it can come into play is if n is drawn. Then you can do the same thing again with one less card that says "what's the most turns it would take to get n to the top of the deck?" Using this backward induction I seem to be getting n - 1 turns, and can't seem to replicate a game with the Fibonacci offset - 1 for any reasonably high numbered deck of cards.2011-04-16
  • 0
    But if $1$ is on the bottom, you stop the turn after you find $n$. The decks I show are better, but I agree you won't reach my Fibonacci bound. Thanks for an interesting problem.2011-04-16
  • 0
    @Ross: I'm not sure your inductive argument holds when n+1 is not at the bottom. The "game" you are playing with the remaining cards is *not* the game you are inducting on, because it does not consist of cards number 1 through n. To take the extreme case Trip suggests, imagine the bottom card is 1, and "n+1 never shows up". Then the resulting n-card game never terminates, because you never see the 1 card. It's not clear in your argument why this cannot be the case.2011-04-16
  • 0
    @Arturo: good point. I was approaching it as I realized that I couldn't achieve the maximum length I claimed as there is an extra card around in the $(n-2)$ case. This is another aspect of the same.2011-04-16
  • 0
    @Ross: I think this fixes it: consider the case where $n+1$ is not the bottom card. Put a sticker on the card with $n+1$ on it with the number of the card at the bottom. Play the game with the top $n$ cards. If you hit $1$ before you hit the sticker, you're done. Otherwise, you will hit the sticker at some point; tear it off and play the $n+1$, and after that turn the game becomes the one you first describe. The same thing happens if $1$ is at the bottom. You "win" the remaining game when $n+1$ (the "fake 1") shows on top, and then you win the game for real once you tear off the sticker.2011-04-16
  • 0
    @Arturo: thanks. This is a better way to express what I was thinking when I said you were playing with two less cards. I think the proof of non-looping in my other answer is also required.2011-04-16
  • 0
    @Arturo: Ross's proof is fine, though might benefit from a little addition. If card $n+1$ is not at the bottom and card $1$ is, then by induction card $n+1$ must appear (since card $1$ appeared in all $n$-card decks); more generally if card $n+1$ is not at the bottom and card $k$ is, we would either find card $n+1$ appearing (problem solved), or find that there are $n$-card decks where card $k$ does not appear but even then card $1$ must appear as part of the inductive hypothesis.2011-04-16
  • 1
    @Ross: I don't see it; the inductive argument seems to me to work, once you substitute the $n+1$. If $k$ is at the bottom and $n+1$ is not on top, place a sticker with $k$ on it on top of the $n+1$ card; *now* you are playing with the top $n$ cards number 1 through n, which by induction terminates. If at some point along the line before hitting 1 it hits the "pretend $k$" card, you tear off the sticker and this places $n+1$ on the bottom and you are back to the first case of your induction. If you get to 1 before hitting the "pretend $k$", it terminates as well.2011-04-16
  • 0
    @Henry: Yes, I agree with that now.2011-04-16
  • 0
    @Arturo: so to get the maximum number of moves I need to find the maximum number of moves in an $n-1$ card deck that runs from $1$ to $n$ missing one number in between, as that is clearly better than having $1$ on the bottom.2011-04-16
1

As Arturo Magidin pointed out, my induction in the last answer doesn't hold, so here is a proof of termination. There are a finite number of orders of $n$ cards without $1$ on top, specifically $n!-(n-1)!.$ If the game does not terminate, we must have a loop that comes back to a previously seen order. Consider $m$, the largest card seen in the course of playing through the loop. After we see it, it goes to position $m$ and no other card in the loop can retrieve it, and we will never see $m$ again. Therefore there is no loop.

  • 0
    Your argument is essentially fine; you just need to refine the induction a bit. See my last comment on that answer; that argument may also help you do the upper bound.2011-04-16
  • 0
    This makes sense, but is there any way to show that that the F(n+1) - 1 rule holds as the maximum number of moves needed to terminate any game? I'm yet to find anything over n = 5 that will make these bounds work.2011-04-16