Skip to main content
1 of 2

Given a permutation $\pi:[n]\to[n]$ of cards from $1$ to $n$ you can find the first card to move to the front the following way:

  1. $c \leftarrow n$
  2. for each $k$ from $n$ down to $1$ do
  3. $\space\space\space$ if $c = \pi(k)$ then
  4. $\space\space\space\space\space\space$ $c \leftarrow c - 1$
  5. $\space\space\space$ end if
  6. end for
  7. return $c$