Relation/ formula to Find the last person standing after removing people alternatively from a line?

84 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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$.

2
On

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