0

Given N number of people, each marked as a number starting from $1$ through N, standing in a straight line where you remove folks alternatively from a line.

For example in iteration 1: You would remove $1,3,5...$

Iteration $2 : 2,6,10...$

etc. until there is one person standing.

Find the index of the last person standing for a given value of N.

3SAT
  • 7,512

2 Answers2

0

$$2^{\lfloor\log_2 N\rfloor}$$

Intelligenti pauca
  • 50,470
  • 4
  • 42
  • 77
0

We can look at the process like this:

  1. Remove the numbers of the form $1+2n$. We are left with multiples of $2$.
  2. Remove the numbers of the form $2+4n$. We are left with multiples of $4$.
  3. Remove the numbers of the form $4+8n$. We are left with multiples of $8$.
  4. Remove the numbers of the form $8+16n$. We are left with multiples of $16$.
  5. Etc.

Therefore, the last number standing is the highest power of $2$ less than or equal to $N$.